プレーヤー②(インプットシステムでプレーヤーを動かす)



シングルトンでInputManagerを作成
using UnityEngine;
public class InputManager : MonoBehaviour
{
private static InputManager instance;
public static InputSystem_Actions isa { get; private set; }
void Awake()
{
if (instance == null)
{
instance = this;
isa = new InputSystem_Actions();
isa.Enable();
DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(this.gameObject);
}
}
void OnDestroy()
{
// 破棄されようとしているのが「本物」の時だけ、インプットを解放する
// (アプリ終了時や、手動でゲーム全体を終了した時など)
if (instance == this)
{
if (isa != null)
{
isa.Disable();
}
}
}
}
Playerを動かす
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float moveSpeed;
private Vector3 pos;
void Update()
{
Vector2 movement2 = InputManager.isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
transform.Translate(movement3 * moveSpeed * Time.deltaTime);
MoveClamp();
}
// 移動範囲の制限
void MoveClamp()
{
pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -10, 10);
pos.z = Mathf.Clamp(pos.z, -10, 10);
transform.position = pos;
}
}
【Unity6版】Danmaku(全20回)
他のコースを見る


シングルトンでInputManagerを作成
using UnityEngine;
public class InputManager : MonoBehaviour
{
private static InputManager instance;
public static InputSystem_Actions isa { get; private set; }
void Awake()
{
if (instance == null)
{
instance = this;
isa = new InputSystem_Actions();
isa.Enable();
DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(this.gameObject);
}
}
void OnDestroy()
{
// 破棄されようとしているのが「本物」の時だけ、インプットを解放する
// (アプリ終了時や、手動でゲーム全体を終了した時など)
if (instance == this)
{
if (isa != null)
{
isa.Disable();
}
}
}
}
Playerを動かす
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float moveSpeed;
private Vector3 pos;
void Update()
{
Vector2 movement2 = InputManager.isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
transform.Translate(movement3 * moveSpeed * Time.deltaTime);
MoveClamp();
}
// 移動範囲の制限
void MoveClamp()
{
pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -10, 10);
pos.z = Mathf.Clamp(pos.z, -10, 10);
transform.position = pos;
}
}
プレーヤー②(インプットシステムでプレーヤーを動かす)