FarmRPG　Unity PlayerBasics Part6 Movement 1/3　キーボード入力でオブジェクトが移動 InputとRigidBody.MovePositionを使う

# 概要
今回はPlayerオブジェクトをwasd入力で動くようにします。
以下実装後の様子です。

![dfasfdfddfasfdsadfsdfsa.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/aa7a2ca8-bd7d-f988-0751-7f5c48807765.gif)


# 開発環境

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

# UnityEditor上の設定
PlayerオブジェクトにRigidBodyとPlayerがアタッチされていることを確認します。



# 実装のポイント
キーボードの入力からC#の変数への変換ををUpgradeメソッド内で実行し、
RigidBodyの処理をfixedUpgradeメソッド内で行います。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/80bfcf52-fc8d-a5b8-48dc-24e8060f0940.png)



斜め移動の係数は三角比を使って算出しています。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/85f93e75-25a7-e877-dd8f-66dc7fea04e4.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 Rigidbody2D rigidBody2D;
    private float movementSpeed;

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

    private void Update()
    {
        PlayerMovementInput();
    }
    
    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)
        {
            movementSpeed = Settings.walkingSpeed;
        }
    }
    
}

```

## Setting

```diff_c_sharp:Setting.cs
using UnityEngine;

public static class Settings 
{
    public const float walkingSpeed = 2.666f;
}

```







# 参考

## C#

## Unity Editor コンポーネント



### Transform.positionとRigidBody.MovePositionの違い
transform.positionで動かすと物理判定が変になるとという程度の理解。（よくわかってない）

https://tat1kun.hatenablog.com/entry/consideration-2022-02-27

## Unity スクリプト

### Input.GetAxis

https://docs.unity3d.com/ja/2021.1/ScriptReference/Input.GetAxis.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/4a4cffbf-39b8-3353-b72c-105191c644f4.png)



### rigidBody.MovePosition

https://docs.unity3d.com/ja/2020.3/ScriptReference/Rigidbody2D.MovePosition.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/0ebe2f5c-7342-61a6-06a9-f2b7c29617c8.png)

### FixedUpdate

物理演算（座標移動）に使う

https://docs.unity3d.com/ja/2021.1/ScriptReference/MonoBehaviour.FixedUpdate.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/4952176d-3eff-7a0b-4595-dfb2287094df.png)


https://docs.unity3d.com/ja/2019.4/Manual/ExecutionOrder.html



## その他

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/7f174ddaacdd9138b7ec3404262128765e602cd4