From 40201a530ac1f5553a9cede5e243259a9cb59054 Mon Sep 17 00:00:00 2001 From: Trey Shaw Date: Tue, 24 Feb 2026 21:26:37 -0600 Subject: [PATCH] add cam rotate with shift for testing --- Assets/Scripts/PlayerController.cs | 59 +++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 7e8711d..782092b 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -1,4 +1,6 @@ +using System.Collections; using UnityEngine; +using static UnityEngine.GraphicsBuffer; public class PlayerController : MonoBehaviour { @@ -12,13 +14,18 @@ public class PlayerController : MonoBehaviour 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 @@ -32,6 +39,14 @@ void Update() Walk(); DigDetector(); + + // TODO: This is just for testing, remove or clean up if used + if (Input.GetKeyDown(KeyCode.LeftShift)) + { + RotateCam(); + } + + HandleCamRotation(); } void Walk() @@ -46,6 +61,16 @@ void Walk() } 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) @@ -80,10 +105,12 @@ void RotatePlayerTowardMouse() { Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(DougBody.transform.position); Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition); + Vector3 newRotation; float angle = Mathf.Atan2(positionOnScreen.y - mouseOnScreen.y, positionOnScreen.x - mouseOnScreen.x) * Mathf.Rad2Deg; + newRotation = new Vector3(0f, -(angle + 90), 0f); - DougBody.transform.rotation = Quaternion.Euler(new Vector3(0f, -(angle + 90), 0f)); + DougBody.transform.rotation = Quaternion.Euler(newRotation); } void TryInteract() @@ -118,4 +145,34 @@ private void OnTriggerExit(Collider other) 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 = 150 * 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; + } + } }