[Unity3D] Playerにアニメーションをつける(BlendTree使用)

1, Capsuleにキャラを子オブジェクトにし、capsuleMeshとcapsuleColliderをRemoveComponentする。
![FEB44D58-2958-4AF1-9C8E-08988C7348C3.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/9334ab4c-8efc-d5a7-ce81-55f62e381379.jpeg)

2, NavMeshAgentを調整し、OverridesからApplyAllを押す
![BB436A6E-8792-4AD5-B773-B783D996D2F5.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/d362fdd6-b004-8462-1f0b-c8f78f3ef6d0.jpeg)

![27787B0D-DE4E-43F6-898C-FD66352FBC91_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/a5ffe74e-3a98-5bf5-e91e-3937860fdc91.jpeg)

3, PlayerにAnimatorとAnimationControllerをつける
![D90BB20D-F6FE-4BAE-84DA-AB54E1E31E23_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/12d9bd60-8a74-8630-0fbc-6df194c8f337.jpeg)

4, AnimatorでBlendTreeを作成する
![05057D9F-8A6B-4BB3-8DFC-5C1869117660_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/4a121f8b-68a7-7651-7a4d-4f187117f642.jpeg)

5, BlendTreeをダブルクリックし、AddMotionFieldを作成する
![482B6A5F-7268-4E59-83AE-60124B62A861_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/c2fa37d4-f786-4d89-f5db-df3e5e3b1036.jpeg)

6, 各Motionを設定する
![F1302FC2-EC99-4704-9B26-167B6C481FCC_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/6211cd06-ba53-6e35-6f27-0d006362bec6.jpeg)

7, PlayerのControllerとAvatarを設定する
![41155F83-9465-4F4F-B63C-C17E716BA782_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/d01eef30-ef3d-647f-936e-00091f28fe09.jpeg)
![AE9C1975-B371-488B-AF51-96DC109D5B49_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/44f17ebd-82c0-cb10-2bfa-040edcd648d3.jpeg)

8, FollowCameraを(0,0,0)に設定し、Playerに合わせ、MainCameraも位置を調整する
![E51D59F4-9E73-446C-8843-137787D1D2DC_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/2c6ed468-21ca-9fdc-9e65-788185239428.jpeg)

スクリプトから動作させる
---
1, BlendTreeのAutomateTfeSholdsのチェックを外し、ComouteThresholds→Velocity Zを選択する
![AC1BDE78-8437-4822-AC96-02DCBDF3222B.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/4278bf59-ae67-3d9e-34ea-b34ab028e847.jpeg)
2, PlayerのSpeedをRunSpeedに合わせる
![AF8621D8-2407-45C9-9B4B-085725DC9D45_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/940521/fadeab89-9eac-95b2-10d9-6284da2ed5d8.jpeg)

3,下記コメント部のスクリプトを追加

```C#:Mover
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Mover : MonoBehaviour
{
    [SerializeField] Transform taget;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            MoveToCursor();
        }
        UpdateAnimator();   //実行
    }

    private void MoveToCursor()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool hasHit = Physics.Raycast(ray, out hit);
        if (hasHit)
        {
            GetComponent<NavMeshAgent>().destination = hit.point;
        }
    }

    //下記を追加
    private void UpdateAnimator()
    {
        Vector3 velocity = GetComponent<NavMeshAgent>().velocity;
        Vector3 localVelocity = transform.InverseTransformDirection(velocity);
        float speed = localVelocity.z;
        GetComponent<Animator>().SetFloat("fowardSpeed", speed);
    }
}
```

追加
---
4, FollowCameraをLateUpdateに変更する

```C#:FollowCamera
void LateUpdate() //LateUpdateに変更
    {
        transform.position = target.position;
    }
```