FarmRPG　Unity PlayerBasics Part7 2/3Playerの歩行を実装する

# 概要
プレイヤーが移動するとそれに対応したアニメーションを再生するにします。
以下gif画像は実装後の様子です。

![ffffffffffffdsafdas.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/4f04ad71-7750-eb3b-a63c-e0f7d9a61eec.gif)



# 開発環境

IDE:Rider
Unity:2020.3.42(LTS)
OS:Windows10

# UnityEditor上の設定

## Playerオブジェクト
PlayerAnimationTestスクリプトはオフにします。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/71ef5af2-e641-bcec-b55a-7e4f445bce32.png)


## Playerオブジェクトの子オブジェクト
下図は子オブジェクトのbodyの画像です。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/afe571a6-5355-31f3-efb5-3e703643ebe7.png)



# 実装のポイント
PlayerクラスのPlayerMovementInputメソッドを修正します。
MovementAnimationParameterControlとSettingクラスもそれに付随して修正します。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/922ecc3c-3687-f437-931b-3ba57b23577d.png)


# コード部分

## Player

```diff_c_sharp:Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : SingletonMonobehaviour<Player>
{
    // Movement Parameters
+    private float xInput;
+    private float yInput;
    private bool isIdle;
+    private bool isWalking;


    private Rigidbody2D rigidBody2D;
    private float movementSpeed;

    protected override void Awake()
    {
        base.Awake();
        rigidBody2D = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        ResetAnimationTriggers();
        
        PlayerMovementInput();
        
        EventHandler.CallMovementEvent(xInput, yInput, isWalking, isIdle,false, false, false, false);
    }
    
    private void FixedUpdate()
    {
        PlayerMovement();
    }

    private void PlayerMovement()
    {
        Vector2 move = new Vector2(xInput * movementSpeed * Time.deltaTime, yInput * movementSpeed * Time.deltaTime);
        rigidBody2D.MovePosition(rigidBody2D.position + move);
    }

    private void PlayerMovementInput()
    {
        // Get player input
        xInput = Input.GetAxisRaw("Horizontal");
        yInput = Input.GetAxisRaw("Vertical");

        // 斜め移動
        if (yInput != 0 && xInput != 0)
        {
            xInput = xInput * 0.71f;
            yInput = yInput * 0.71f;
        }

        // 一方向の移動
        if (xInput != 0 || yInput != 0)
        {
+            isWalking = true;
+            movementSpeed = Settings.walkingSpeed;
+            isIdle = false;
        }
        else if (xInput == 0 && yInput == 0)
        {
            isWalking = false;
            isIdle = true;
        }
    }
}

```


## EventHandler

```diff_c_sharp:EventHandler.cs
public delegate void MovementDelegate(float inputX, float inputY, bool isWalking, bool isIdle,bool idleUp, bool idleDown, bool idleLeft, bool idleRight);

public static class EventHandler
{
    // Movement Event

    public static event MovementDelegate MovementEvent;

    // Movement Event Call For Publishers

    public static void CallMovementEvent(float inputX, float inputY, bool isWalking,bool isIdle,bool idleUp, bool idleDown, bool idleLeft, bool idleRight)
    {
        if (MovementEvent != null)
            MovementEvent(inputX, inputY,isWalking,isIdle,idleUp, idleDown, idleLeft, idleRight);
    }
}

```

## MovementAnimationParameterControl

```diff_c_sharp:MovementAnimationParameterControlr.cs

using UnityEngine;

public class MovementAnimationParameterControl : MonoBehaviour
{
    private Animator animator;

    // Use this for initialisation

    private void Awake()
    {
        animator = GetComponent<Animator>();
    }

    private void OnEnable()
    {
        EventHandler.MovementEvent += SetAnimationParameters;
    }

    private void OnDisable()
    {
        EventHandler.MovementEvent -= SetAnimationParameters;
    }

    private void SetAnimationParameters(float xInput, float yInput, bool isWalking, bool isIdle, bool idleUp, bool idleDown, bool idleLeft, bool idleRight)
    {
+        animator.SetFloat(Settings.xInput, xInput);
+        animator.SetFloat(Settings.yInput, yInput);
+        animator.SetBool(Settings.isWalking, isWalking);
        if (idleUp)
            animator.SetTrigger(Settings.idleUp);
        if (idleDown)
            animator.SetTrigger(Settings.idleDown);
        if (idleLeft)
            animator.SetTrigger(Settings.idleLeft);
        if (idleRight)
            animator.SetTrigger(Settings.idleRight);
    }


}


```


## Setting

```diff_c_sharp:Setting.cs


using UnityEngine;

public static class Settings 
{
    // Player Movement
+    public const float walkingSpeed = 2.666f;
    
    // Player Animation Parameters
+    public static int xInput;
+    public static int yInput;
+    public static int isWalking;

    // Shared Animation Parameters
    public static int idleUp;
    public static int idleDown;
    public static int idleLeft;
    public static int idleRight;

    // static constructor
    static Settings()
    {
        // Player Animation Parameters
+       xInput = Animator.StringToHash("xInput");
+       yInput = Animator.StringToHash("yInput");
+       isWalking = Animator.StringToHash("isWalking");
        // Shared Animation parameters
        idleUp = Animator.StringToHash("idleUp");
        idleDown = Animator.StringToHash("idleDown");
        idleLeft = Animator.StringToHash("idleLeft");
        idleRight = Animator.StringToHash("idleRight");
    }
}
```



# 参考

## C#

## Unity Editor コンポーネント

## Unity スクリプト

### Animator.SetFloat

https://docs.unity3d.com/ja/2019.4/ScriptReference/Animator.SetFloat.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/40c9952d-985f-7721-459b-e9706a5134cf.png)



## その他

Section5　12 Basic Player Movement

https://www.udemy.com/course/unity-2d-game-developer-course-farming-rpg/

github コミット分（個人確認用　privateなので見れません）

https://github.com/RYA234/LearnFarmRPG/commit/0dcb94a0dc6ebf813952922930309522250dbc7a