[Unity3D] Cinematicsでのアニメーション映像作成

フォローカメラが設定されている状態から始める

1, ヒエラルキーに空のCinematicsを作成し、ProjectにもCnematicsフォルダを作成。
FollowCameraのPriorityを100にする。
![FF6C7C03-C609-48A5-B39E-F830957597D3.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/2158431f-ff15-f17d-ab9e-21e58e1be7cb.jpeg)

2, CinemachineのCreateVirtualCameraを選択する
![A4CC2B83-8281-49D2-9224-D55011BA9412_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/d354118a-4f21-7f90-4d30-f85515442bdc.jpeg)

3, CM Reveal1に名前を変更し、Soloを押してカメラ位置を合わせていく
![673EBB4A-56F2-473A-A1A9-6CFAB176452D.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/e11f2434-d475-93fb-2d56-fcd4eecaa8f7.jpeg)

4, CM Reveal1のLookAtに映す物を設定する
![A22AD31E-0ADB-4186-B753-DED997BD0EBF.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/11e8342a-67be-0838-6138-7209e9a284d1.jpeg)

・再生を押してSoloを押すと確認できる
![DA973D84-35B5-453B-A0D3-3364F6BD12AD.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/b4224225-0e57-9cc0-9e75-f7862f422201.jpeg)

5, WindowのSequencing→timelinesを選択する
![9CD882B5-9284-4C7C-B5FF-92EAAA38052A_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/b88e0ee5-2809-913d-aab7-2c74a0408a97.jpeg)

6, intoSequenceを選択した状態でCreateを押す
![A5FD8879-841E-4BDB-8239-06978CCED53A_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/d959256e-543a-8218-d11c-88fdd6c42b02.jpeg)

7, 任意の場所に保存する
![376CF9D1-8E5B-4E58-818F-188539239E13_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/078ef490-038c-4b6c-fd6e-408fef4cfda8.jpeg)

8, CM　Reveal1をスペースにドラッグし、Cameraを選択する
![E5555832-9805-4095-A786-23F5B7D9B259_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/ce09626b-5bb1-9b95-e191-29510ad10912.jpeg)
![96219E50-D4DD-4AB4-8B41-FAE4B56DAF68_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/eacaec2a-69f8-de20-1dde-c2d2f7e8587c.jpeg)

9, 別のCinemaとくっつける場合は同位置にドラッグしブレンドする
![EF095DF4-7200-497F-AAC7-27F4B8350ADF_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/dbf00ee1-26ae-085a-5c82-dd3d59e545f9.jpeg)

・他にくっつけたいものがあれば同様に作成する

DollyCameraでの実装
---

1, WindowのCreateDollyCameraWithTrackを選択する
![6E9BE9E7-083D-448E-88A5-7DE97F50DD31_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/16199b31-8b62-a250-0751-e6abf7def87c.jpeg)

2, DollyTrackをプラスボタンで増やしながら作成する
![54798E25-8259-405B-BD3F-E995F51E09B9.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/bb148a77-e6cd-2fdb-6525-2614a0cc5875.jpeg)

3, CMDolly1のSoloを押しカメラを合わせ、Distanceにする
![6B5A815D-4CDB-44C8-A3B8-E3C3852869CF.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/337e4e19-eb74-6663-8848-7b8d09925f2b.jpeg)

4, CMDolly1にオブジェクトを設定し、PathPosition、Normalizadに設定する
![B37C3F34-B9BD-4F1D-BB79-9DE0B10D8BB3.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/5a5ce66b-80ec-9e4f-00ca-3e386ea936cc.jpeg)

5, CMDolly1をTimeLineにドラッグする.空の部分にもドラッグし、アニメータを作成しておく。
![EC051B0F-3324-4AE5-A557-99FD8F5AD198_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/132e3467-238d-7f25-0fd8-41dedaeb808c.jpeg)

6, CMDolly1のPathPositionをゼロ０にする
![67FFBF29-F6F7-4155-82C4-8998E5442B17.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/dbe513de-e31c-734d-9e1a-2f0b6bb7e1b9.jpeg)

7, CMDolly1の終わり間際の部分でRecodingを押す
![C9EAE65A-B9F1-4B0C-97C5-B3F1E85CAE53_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/3a175416-e3ef-c6e7-ee66-116b3b14e8e5.jpeg)

8, Recodingが始まったらCMDolly1のPathPositionを1にする
![E842F5B3-2853-4FE2-9D4C-AB871F84D131.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/74252aed-ed3f-3dd7-fb2c-e4efdac44e5d.jpeg)

9, Recoding状態のままCMDolly1の始まり部分を選択する
![3B34A2A7-BEAB-4D2C-B4CB-2AF91EAFFC8B.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/c89e05aa-2b2d-713e-b88f-a47a9e27764a.jpeg)

10, PathPositionをゼロ０にする
![022EF50A-602F-481B-8B08-B739F1106BAE.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/f298e1ad-31f0-ad87-59cf-9ec320180b31.jpeg)

11, Recodingボタンを押し、停止する。

12, CMReveal2に合わせてから※マーク→Secondsを押す
![7785F111-CC5A-4540-9D14-95EFD8546CAB_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/66d3db8f-fb58-f8ec-e5dc-b6bda5e15d52.jpeg)

13, CMDolly1の開始部分に合わせる(開始時間を覚えておく)
![91412202-0B81-42CD-9296-DCD32D206B66_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/914aed3b-4ab6-7011-9b3d-b0bb2a352f57.jpeg)

14, この間をダブルクリックするとAnimationが開かれる
![E8B39745-389A-422A-8525-B93DA0A54FDF_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/d6454a35-0a24-4f5d-22c4-2d10b5520629.jpeg)

15, CmDolly１の開始時間に合わせてイベントが発生するように位置を合わせる
![46A2CA4F-A78E-48A8-B2DF-4E93CF87ACF2_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/ed99dd19-15cc-7a1f-c852-1974b8ae5ff8.jpeg)

Triggerでアニメーションを実行する
---
1, introSequenceにBoxcolliderをつける
![CFC188B3-9E69-4305-9079-2146DC949D1E.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/711e369a-50fd-bba0-9dd9-0d1181356fd7.jpeg)

2, isTriggerにチェックを入れ、サイズを調整する
![41EB529D-D355-4D71-BE5D-9D222E390196.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/5a420966-a814-aa85-64c6-d8e05431faaa.jpeg)

3, introSequenceにrigidBodyをつけ、Grabityは外し、Kinematicはつける
![840C8449-46B7-4ADC-92D5-C47C5B077A0E_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/3fe6dae2-e537-6916-1d6e-e5d4ff0237da.jpeg)

4, PlayerにCapsuleColliderをつけ、サイズを調整する
![97911C5F-6928-4682-8E35-D0B0350C4607.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/368e5c60-95e4-e16e-ede4-21e38971ad43.jpeg)

5, IntroSequenceのPlayOnAwakeを外す
![CC194E34-C261-4EF5-87A0-B7350D4A7ABA.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/306d2cdf-97de-1d6c-f413-90b89eb786c1.jpeg)

6, IntroSequenceのLayer→IgnoreRayCastに設定する
![F9A502F7-C1A6-404E-8AD1-6AB145559A03_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/47e8e4a1-2354-2183-5158-f24cdb93c005.jpeg)

7, CinematicTriggerスクリプトを作成し、下記のコードを記述する
**（Tagも使って呼び出しているのでplayerにはPlayerTagを設定しておくこと）**

```C#:CinematicTrigger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;


namespace RPG.Cinematics
{
    public class CinematicTrigger : MonoBehaviour
    {
        bool alreadyTriggered = false;

        private void OnTriggerEnter(Collider other)
        {
            if (!alreadyTriggered && other.gameObject.tag == "Player")
            {
                alreadyTriggered = true;
                GetComponent<PlayableDirector>().Play();
            }
        }
    }
}
```

8, IntroSequenceにCinematicTriggerスクリプトをアタッチする
![90103350-9765-4884-A703-DEED382955A4.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/4e5578d8-de5d-8365-8370-51a73b3f36db.jpeg)

イベントが発生したらplayerを立ち止まらせる
---

1, CinematicControlRemoverスクリプトを作成し、IntroSequenceにアタッチする
![B9BDF805-EC01-4ACC-9307-7775908470D6.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/58509b7e-0222-8cb6-91c6-5b068bcf0f4f.jpeg)

2, CinematicControlRemoverとFighterに下記コードを記述する

```C#:CinematicControlRemover
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

namespace RPG.Cinematics
{
    public class CinematicControlRemover : MonoBehaviour
    {
        private void Start()
        {
            GetComponent<PlayableDirector>().played += DisableControl;
            GetComponent<PlayableDirector>().stopped += EnableControl;
        }

        void DisableControl(PlayableDirector pd)
        {
            Debug.Log("DisableControl");
        }

        void EnableControl(PlayableDirector pd)
        {
            Debug.Log("EnableControl");
        }
    }
}
```

```C#:Fighter
using UnityEngine;
using RPG.Movement;
using RPG.Core;

namespace RPG.Combat
{
    public class Fighter : MonoBehaviour, IAction
    {
        [SerializeField] float weaponRange = 2f;
        [SerializeField] float timeBetweenAttacks = 1f;
        [SerializeField] float weaponDamage = 5f;

        Health target;
        float timeSinceLastAttack = Mathf.Infinity;

        private void Update()
        {
            timeSinceLastAttack += Time.deltaTime;

            if (target == null) return;
            if (target.IsDead()) return;

            if (!GetIsInRange())
            {
                GetComponent<Mover>().MoveTo(target.transform.position, 1f); //1fを追加
            }
            else
            {
                GetComponent<Mover>().Cancel();
                AttackBehaviour();
            }
        }

        private void AttackBehaviour()
        {
            transform.LookAt(target.transform);
            if (timeSinceLastAttack > timeBetweenAttacks)
            {
                TriggerAttack();
                timeSinceLastAttack = 0;
            }
        }

        private void TriggerAttack()
        {
            GetComponent<Animator>().ResetTrigger("stopAttack");
            GetComponent<Animator>().SetTrigger("attack");
        }

        void Hit()
        {
            if(target == null) { return; }
            target.TakeDamage(weaponDamage);
        }

        private bool GetIsInRange()
        {
            return Vector3.Distance(transform.position, target.transform.position) < weaponRange;
        }

        public bool CanAttack(GameObject combatTarget)
        {
            if (combatTarget == null) { return false; }
            Health targetToTest = combatTarget.GetComponent<Health>();
            return targetToTest != null && !targetToTest.IsDead();
        }

        public void Attack(GameObject combatTarget)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            target = combatTarget.GetComponent<Health>();
        }

        public void Cancel()
        {
            StopAttack();
            target = null;
            GetComponent<Mover>().Cancel(); //追加
        }

        private void StopAttack()
        {
            GetComponent<Animator>().ResetTrigger("attack");
            GetComponent<Animator>().SetTrigger("stopAttack");
        }
    }
}
```