LoginSignup
0
0

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

Last updated at Posted at 2023-05-26

概要

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

ffffffffffffdsafdas.gif

開発環境

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

UnityEditor上の設定

Playerオブジェクト

PlayerAnimationTestスクリプトはオフにします。
image.png

Playerオブジェクトの子オブジェクト

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

image.png

実装のポイント

PlayerクラスのPlayerMovementInputメソッドを修正します。
MovementAnimationParameterControlとSettingクラスもそれに付随して修正します。

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

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

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

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

image.png

その他

Section5 12 Basic Player Movement

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

0
0
0

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
  3. You can use dark theme
What you can do with signing up
0
0