fix lookat camera

This commit is contained in:
2026-03-26 21:39:17 -05:00
parent 1e0ea14ae2
commit 6e22731e50
+10 -8
View File
@@ -20,18 +20,25 @@ public class PlayerController : MonoBehaviour
private bool hasControl = true; // set this to false if we want to stop reading player inputs
private bool isSprinting = false;
// Mathematical plane used to catch the raycast from camera to get direction for
// looking at the mouse
private Plane groundPlane;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
groundPlane = new Plane(Vector3.up, -DougBody.transform.position.y);
characterController = GetComponent<CharacterController>();
cameraController = GetComponent<CameraController>();
SwitchTools(GameManager.Instance.PlayerManager.CurrentToolIndex);
}
// Update is called once per frame
void Update()
{
moveDir = Vector3.zero;
groundPlane.distance = -DougBody.transform.position.y;
if (hasControl)
{
@@ -201,17 +208,12 @@ void ToolUseDetector()
void RotatePlayerTowardMouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
if (groundPlane.Raycast(ray, out var distance))
{
Vector3 targetPosition = hit.point;
targetPosition.y = transform.position.y;
Vector3 hitPoint = ray.GetPoint(distance);
Vector3 direction = targetPosition - transform.position;
Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);
DougBody.transform.rotation = targetRotation;
DougBody.transform.LookAt(new Vector3(hitPoint.x, DougBody.transform.position.y, hitPoint.z));
}
}