FarmRPG　Unity PlayerBasics Part8  3/3 Playerの走行を実装する

# 概要
今回はプレイヤーの走行を実装します。具体的には
Shift押しながら移動→歩行
Shift押さないで移動→歩行
するように実装します。

以下gif画像は実装後の様子です。

![dffffffffffffdasdffdsa.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/d56d80ef-089a-9e0c-692d-4928959827a3.gif)



# 開発環境

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

# UnityEditor上の設定
　スクリプトの変更のみ

# 実装のポイント
Playerクラスに新規メソッドPlayerWalkInputを加えます。
PlayerWalkInput

Dで右側に走行するときの処理
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/7dbdfbc7-9f74-ac67-cf00-94c21e9415bb.png)

Shift+Dで右側に歩行するときの処理

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/bd96e3be-e381-0e2e-1ea6-2156d137c910.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 bool isRunning;


    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,isRunning, 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;
+            PlayerWalkInput()
        }

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

+    private void PlayerWalkInput()
+    {
+        if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
+        {
+            isRunning = false;
+            isWalking = true;
+            isIdle = false;
+            movementSpeed = Settings.walkingSpeed;
+        }
+        else
+        {
+            isRunning = true;
+            isWalking = false;
+            isIdle = false;
+            movementSpeed = Settings.runningSpeed;
+        }
+    }
    
}

```


## EventHandler

```diff_c_sharp:EventHandler.cs
public delegate void MovementDelegate(float inputX, float inputY, bool isWalking, bool isRunnig, 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 isRunnig,bool isIdle,bool idleUp, bool idleDown, bool idleLeft, bool idleRight)
    {
        if (MovementEvent != null)
            MovementEvent(inputX, inputY,isWalking,isRunning,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 isRunning,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);
+        animator.SetBool(Settings.isRunning, isRunning);
        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 runningSpeed = 5.333f;
    public const float walkingSpeed = 2.666f;
    
    // Player Animation Parameters
    public static int xInput;
    public static int yInput;
    public static int isWalking;
+    public static int isRunning;

    // 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");
+        isRunning = Animator.StringToHash("isRunning");
        // Shared Animation parameters
        idleUp = Animator.StringToHash("idleUp");
        idleDown = Animator.StringToHash("idleDown");
        idleLeft = Animator.StringToHash("idleLeft");
        idleRight = Animator.StringToHash("idleRight");
    }
}
```



# 参考

## C#

## Unity Editot コンポーネント

## Unity スクリプト

### Input.GetKey

https://docs.unity3d.com/ja/2019.4/ScriptReference/Input.GetKey.html


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/90a1a518-208c-4873-a54c-0bb69635bed6.png)



### KeyCode

https://docs.unity3d.com/ja/2021.2/ScriptReference/KeyCode.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/c9d10c5d-e8a1-2bb4-7989-eeea827f892f.png)


## その他

Section5 12 Basic Player Movment

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

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

https://github.com/RYA234/LearnFarmRPG/commit/40291611bb3ff2c84991adc7c520759e76a2f267