テストキャラを作成して動かす(シングルトンでInputSystemを管理する)



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();
}
}
}
}




TestMove
using UnityEngine;
public class TestMove : MonoBehaviour
{
private float moveSpeed = 5f;
private float turnSpeed = 50f;
void Update()
{
Vector2 movement2 = InputManager.isa.Player.Move.ReadValue<Vector2>();
transform.Translate(Vector3.forward * movement2.y * Time.deltaTime * moveSpeed);
transform.Rotate(Vector3.up * movement2.x * Time.deltaTime * turnSpeed);
}
}

【Unity6版】BattleOnline(全38回)
他のコースを見る


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();
}
}
}
}




TestMove
using UnityEngine;
public class TestMove : MonoBehaviour
{
private float moveSpeed = 5f;
private float turnSpeed = 50f;
void Update()
{
Vector2 movement2 = InputManager.isa.Player.Move.ReadValue<Vector2>();
transform.Translate(Vector3.forward * movement2.y * Time.deltaTime * moveSpeed);
transform.Rotate(Vector3.up * movement2.x * Time.deltaTime * turnSpeed);
}
}

テストキャラを作成して動かす(シングルトンでInputSystemを管理する)