以下は、Input Managerを使ってプレイヤーキャラクターを移動させる基本的なスクリプトの例です。
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 5f; // プレイヤーの移動速度
void Update()
{
// ユーザーの入力を取得
float moveHorizontal = Input.GetAxis("Horizontal"); // 左右の入力
float moveVertical = Input.GetAxis("Vertical"); // 前後の入力
// プレイヤーの移動ベクトルを作成
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
// プレイヤーを移動させる
transform.Translate(movement * speed * Time.deltaTime);
}
}
Input.GetAxis("Horizontal")
と Input.GetAxis("Vertical")
は、Input Managerで設定した「Horizontal」と「Vertical」にマッピングされた入力を取得します。movement
ベクトルにspeed
とTime.deltaTime
を掛けることで、フレームレートに依存しない移動速度を実現しています。