using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { [SerializeField] private float moveSpeed = 7f; [SerializeField] private GameInput gameInput; [SerializeField] private LayerMask countersLayerMask; private bool isWalking; private Vector3 lastInteractDir; private void Start() { gameInput.OnInteractAction += GameInput_OnInteractAction; } private void GameInput_OnInteractAction(object sender, System.EventArgs e) { Vector2 inputVector = gameInput.GetMovementVectorNormalized(); Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y); // Kepp a constantly updating record of the last move direction. // This means you can interact without holding down a direction. if (moveDir != Vector3.zero) { lastInteractDir = moveDir; } float interactDistance = 2f; if (Physics.Raycast(transform.position, lastInteractDir, out RaycastHit raycastHit, interactDistance, countersLayerMask)) { if (raycastHit.transform.TryGetComponent(out ClearCounter clearCounter)) { // Has Clear Counter clearCounter.Interact(); } } } private void Update() { HandleMovement(); HandleInteractions(); } public bool IsWalking() { return isWalking; } /// /// Handle object interactions. /// private void HandleInteractions() { Vector2 inputVector = gameInput.GetMovementVectorNormalized(); Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y); // Kepp a constantly updating record of the last move direction. // This means you can interact without holding down a direction. if (moveDir != Vector3.zero) { lastInteractDir = moveDir; } float interactDistance = 2f; if (Physics.Raycast(transform.position, lastInteractDir, out RaycastHit raycastHit, interactDistance, countersLayerMask)) { if (raycastHit.transform.TryGetComponent(out ClearCounter clearCounter)) { // Has Clear Counter //clearCounter.Interact(); } } } /// /// Handle the player movement and collision. /// private void HandleMovement() { Vector2 inputVector = gameInput.GetMovementVectorNormalized(); Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y); float moveDistance = moveSpeed * Time.deltaTime; float playerRadius = .7f; float playerHeight = 2f; bool canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDir, moveDistance); if (!canMove) { // Cannot move towards moveDir // Attempt only X move Vector3 moveDirX = new Vector3(moveDir.x, 0, 0).normalized; canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDirX, moveDistance); if (canMove) { moveDir = moveDirX; } else { // Cannot move on X so attempt to move on Z Vector3 moveDirZ = new Vector3(0, 0, moveDir.z).normalized; canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDirZ, moveDistance); if (canMove) { moveDir = moveDirZ; } else { // Cannot move at all } } } if (canMove) { transform.position += moveDir * moveDistance; } isWalking = moveDir != Vector3.zero; float rotateSpeed = 10f; transform.forward = Vector3.Slerp(transform.forward, moveDir, Time.deltaTime * rotateSpeed); } }