using UnityEngine; using UnityEngine.InputSystem; [RequireComponent(typeof(CharacterController))] public class PlayerMovement : MonoBehaviour { [Header("Movement Settings")] [SerializeField] private float movementSpeed = 5f; [SerializeField] private float rotationSpeed = 200f; [SerializeField] private InputActionReference movement; [SerializeField] private Transform cameraTransform; [SerializeField] private Vector3 cameraOffset = new Vector3(0, 10, -10); [SerializeField] private Animator _animator; [Header("Footstep Sound Settings")] [SerializeField] private AK.Wwise.Event footstepEvent; [SerializeField] private float footstepInterval = 0.5f; // seconds between footsteps private float footstepTimer = 0f; private Vector3 inputDirection = Vector3.zero; private CharacterController controller; void Start() { controller = GetComponent(); movement.action.Enable(); movement.action.performed += OnMovement; movement.action.canceled += OnMovementStop; } void OnMovement(InputAction.CallbackContext context) { Vector2 inputVector = context.ReadValue(); inputDirection = new Vector3(inputVector.x, 0, inputVector.y); } void OnMovementStop(InputAction.CallbackContext context) { inputDirection = Vector3.zero; } void Update() { Cursor.lockState = CursorLockMode.Locked; Vector3 cameraForward = cameraTransform.forward; Vector3 cameraRight = cameraTransform.right; cameraForward.y = 0; cameraRight.y = 0; cameraForward.Normalize(); cameraRight.Normalize(); Vector3 move = (cameraForward * inputDirection.z + cameraRight * inputDirection.x).normalized * movementSpeed; controller.SimpleMove(move); _animator.SetFloat("Speed", move.magnitude); if (move.magnitude > 0.01f) { Quaternion toRotation = Quaternion.LookRotation(move, Vector3.up); transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime); // Footstep logic footstepTimer -= Time.deltaTime; if (footstepTimer <= 0f && footstepEvent != null) { footstepEvent.Post(gameObject); footstepTimer = footstepInterval; } } else { footstepTimer = 0f; // Reset when not moving } } void LateUpdate() { if (cameraTransform != null) { cameraTransform.position = transform.position + cameraOffset; cameraTransform.LookAt(transform.position); } } }