51 lines
1.1 KiB
C#
Raw Normal View History

2025-09-19 19:43:49 +05:00
using UnityEngine;
namespace UnityHFSM.Samples.Sample3d
{
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
public static PlayerController Instance { get; private set; }
[SerializeField]
private float speed = 2;
private Rigidbody rb;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
Instance = this;
}
}
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
Vector3 playerInput = new Vector3(
Input.GetAxis("Horizontal"),
0f,
Input.GetAxis("Vertical")
);
rb.velocity = playerInput * speed;
}
private void OnDestroy()
{
if (Instance == this)
{
Instance = null;
}
}
}
}