using System; using UnityEngine; using UnityEngine.InputSystem; namespace NonSolid { [RequireComponent(typeof(CharacterController))] public class PlayerController : MonoBehaviour { [Header("Movement")] [SerializeField] private float _speed = 10.0f; [SerializeField] private float _rotationSpeed = 60.0f; [Header("Firing")] [SerializeField] private Transform _shootSpawn; [SerializeField] private Rigidbody _projectilePrefab; [SerializeField] private float _shootForce = 10.0f; [SerializeField] private FireType _fireType = FireType.Bullet; [Header("Inputs")] [SerializeField] private InputActionReference _movementInputAction; [SerializeField] private InputActionReference _rotationInputAction; [SerializeField] private InputActionReference _fireInputAction; private CharacterController _characterController; private Vector2 _movement; private Vector2 _rotation; private void Awake() { _characterController = GetComponent(); } private void OnEnable() { _movementInputAction.action.Enable(); _rotationInputAction.action.Enable(); _fireInputAction.action.Enable(); _fireInputAction.action.performed += OnFireActionPerformed; } private void OnDisable() { _movementInputAction.action.Disable(); _rotationInputAction.action.Disable(); _fireInputAction.action.Disable(); _fireInputAction.action.performed -= OnFireActionPerformed; } private void Update() { HandleInput(); MoveAndRotate(); } private void HandleInput() { _movement = _movementInputAction.action.ReadValue(); _rotation = _rotationInputAction.action.ReadValue(); } private void MoveAndRotate() { transform.Rotate(0, _rotation.x * _rotationSpeed * Time.deltaTime, 0); _characterController.Move(new Vector3(_movement.x, 0, _movement.y) * (_speed * Time.deltaTime)); } private void Fire() { switch (_fireType) { case FireType.Bullet: Rigidbody projectile = Instantiate(_projectilePrefab, _shootSpawn.position, _shootSpawn.rotation); projectile.AddForce(_shootSpawn.forward * _shootForce, ForceMode.Impulse); break; case FireType.Spell: break; case FireType.Arrow: break; default: throw new ArgumentOutOfRangeException(); } } private void OnFireActionPerformed(InputAction.CallbackContext ctx) { Fire(); } } }