using System.Collections; using UnityEngine; using static UnityEngine.GraphicsBuffer; public class PlayerController : MonoBehaviour { public GameObject DougBody; public Shovel Shovel; public float walkSpeed; float digTime = 0.9f; float digTimestamp = 0; bool isDigging = false; Interactable nearestInteractable; private CharacterController characterController; private CameraController cameraController; private Vector3 moveDir; private float gravity = 10; private float camTargetRotation = 0; private bool isCamRotating = false; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { characterController = GetComponent(); cameraController = GetComponent(); } // Update is called once per frame void Update() { if (!isDigging) { RotatePlayerTowardMouse(); TryInteract(); } Walk(); DigDetector(); // TODO: This is just for testing, remove or clean up if used if (Input.GetKeyDown(KeyCode.LeftShift)) { RotateCam(); } HandleCamRotation(); } void Walk() { float verticalMovement = 0; float horizontalMovement = 0; if (!isDigging) { verticalMovement = Input.GetAxisRaw("Vertical"); horizontalMovement = Input.GetAxisRaw("Horizontal"); } moveDir = new Vector3(horizontalMovement, 0, verticalMovement).normalized; if (cameraController != null) { // re-matrix the rotation direction so movement is consistent no matter what // the camera rotation is Matrix4x4 matrix = Matrix4x4.Rotate(Quaternion.Euler(0, cameraController.playerCamHome.rotation.eulerAngles.y, 0)); moveDir = matrix.MultiplyPoint3x4(moveDir); } moveDir *= walkSpeed; if (!characterController.isGrounded) { moveDir.y = -gravity; } characterController.Move(moveDir * Time.deltaTime); } void DigDetector() { if (!isDigging) { if (Input.GetMouseButtonDown(0)) { Shovel.Dig(); isDigging = true; digTimestamp = Time.time + digTime; // fuck coroutines fr fr } } else { if (Time.time > digTimestamp) { isDigging = false; } } } void RotatePlayerTowardMouse() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { Vector3 targetPosition = hit.point; targetPosition.y = transform.position.y; Vector3 direction = targetPosition - transform.position; Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up); DougBody.transform.rotation = targetRotation; } } void TryInteract() { if (Input.GetKeyDown(KeyCode.E) && nearestInteractable != null) { nearestInteractable.Interact(); } } private void OnTriggerEnter(Collider other) { Interactable interactable = other.GetComponent(); // store nearest interactable if it exists if (interactable != null) { nearestInteractable = interactable; interactable.MoveInsideRange(); } } private void OnTriggerExit(Collider other) { // just null out interactables when we leave an interactable trigger (we cant and shouldn't overlap interactables) Interactable interactable = other.GetComponent(); // store nearest interactable if it exists if (interactable != null && nearestInteractable != null) { nearestInteractable.MoveOutsideRange(); nearestInteractable = null; } } void RotateCam() { if (!isCamRotating) { float currentY = cameraController.playerCamHome.transform.rotation.eulerAngles.y; camTargetRotation = currentY + 90f; isCamRotating = true; } } void HandleCamRotation() { float currentY = cameraController.playerCamHome.transform.rotation.eulerAngles.y; float step = 200 * Time.deltaTime; float newY = Mathf.MoveTowardsAngle(currentY, camTargetRotation, step); cameraController.playerCamHome.RotateAround( transform.position, Vector3.up, newY - currentY ); if (Mathf.Abs(Mathf.DeltaAngle(newY, camTargetRotation)) < 0.01f) { isCamRotating = false; } } }