
キャラクターに後退や方向転換をさせる方法がわかりません。

悩むことはないよ!
これまで学習した内容に一手間加えるだけで、実現できるよ!
引き続き、Input Systemを使います。
Input Systemは、ゲームパッド・キーボード・マウス・センサー等の各種入力デバイスからの入力を汎用的・統合的に取り扱うために導入されたUnityの新しい入力システムです。
Unity for Pro
今回は、キーボード操作で、後退・方向転換をさせる方法について解説していきます。
では、早速始めていきましょう!
プロジェクトは前回の続きから始めていきますので、手順に不安のある方は以下の記事内容を先に実施してから、戻ってきてください。
ここでは、キャラクターとアニメーションは、mixamoを使用します。
後退
ここでは、前進するアニメーションを使って、後退させる方法について解説していきます。
スクリプトの編集
キャラクターに付けたスクリプトを微修正します。
Projectウインドウから、Scriptを開いて、以下のコードを入力します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class WorkerController : MonoBehaviour
{
Vector3 moveDirection;
public float moveSpeed = 2;
public float maxForwardSpeed = 8;
float desiredSpeed;
float forwardSpeed;
const float groundAccel = 5;
const float groundDecel = 25;
Animator anima;
bool IsMoveInput
{
get { return !Mathf.Approximately(moveDirection.sqrMagnitude, 0f); }
}
public void OnMove(InputAction.CallbackContext context)
{
moveDirection = context.ReadValue<Vector2>();
}
void Move(Vector2 direction)
{
float fDirection = direction.y;
if (direction.sqrMagnitude > 1f)
direction.Normalize();
desiredSpeed = direction.magnitude * maxForwardSpeed * Mathf.Sign(fDirection);
float acceleration = IsMoveInput ? groundAccel : groundDecel;
forwardSpeed = Mathf.MoveTowards(forwardSpeed, desiredSpeed, acceleration * Time.deltaTime);
anima.SetFloat("ForwardSpeed", forwardSpeed);
}
void Start()
{
anima = this.GetComponent<Animator>();
}
void Update()
{
Move(moveDirection);
}
}
編集が完了したら、保存します。
Blend Treeの変更
Animatorウインドウで、[Blend Tree]をクリックして、Inspectorウインドウで、[Changes animation speed.]にマイナスの値を入れます。

動作確認
[▶︎]プレイゲームをクリックして、キーボードの矢印キーを操作します。
アニメーションを使って後退をさせることができました。
方向転換
Projectウインドウから、Scriptを開いて、以下のコードを入力します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class WorkerController : MonoBehaviour
{
Vector3 moveDirection;
public float moveSpeed = 2;
public float maxForwardSpeed = 8;
public float turnSpeed = 50;
float desiredSpeed;
float forwardSpeed;
const float groundAccel = 5;
const float groundDecel = 25;
Animator anima;
bool IsMoveInput
{
get { return !Mathf.Approximately(moveDirection.sqrMagnitude, 0f); }
}
public void OnMove(InputAction.CallbackContext context)
{
moveDirection = context.ReadValue<Vector2>();
}
void Move(Vector2 direction)
{
float turnAngle = direction.y;
float fDirection = direction.y;
if (direction.sqrMagnitude > 1f)
direction.Normalize();
desiredSpeed = direction.magnitude * maxForwardSpeed * Mathf.Sign(fDirection);
float acceleration = IsMoveInput ? groundAccel : groundDecel;
forwardSpeed = Mathf.MoveTowards(forwardSpeed, desiredSpeed, acceleration * Time.deltaTime);
anima.SetFloat("ForwardSpeed", forwardSpeed);
transform.Rotate(0, turnAngle *turnSpeed * Time.deltaTime, 0);
}
// Start is called before the first frame update
void Start()
{
anima = this.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Move(moveDirection);
}
}
編集が完了したら、保存します。
動作確認
[▶︎]プレイゲームをクリックして、キーボードの矢印キーを操作します。
方向転換をさせることができました。
以上です。お疲れ様でした。