add cam rotate with shift for testing

This commit is contained in:
2026-02-24 21:26:37 -06:00
parent 6509a5e78f
commit 40201a530a
+58 -1
View File
@@ -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<CharacterController>();
cameraController = GetComponent<CameraController>();
}
// 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;
}
}
}