LoginSignup
0

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

Last updated at Posted at 2023-05-26

概要

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

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

dffffffffffffdasdffdsa.gif

開発環境

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

UnityEditor上の設定

 スクリプトの変更のみ

実装のポイント

Playerクラスに新規メソッドPlayerWalkInputを加えます。
PlayerWalkInput

Dで右側に走行するときの処理
image.png

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

image.png

コード部分

Player

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

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

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

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

image.png

KeyCode

image.png

その他

Section5 12 Basic Player Movment

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0