diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index c0ce489..b6943cd 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -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(); cameraController = GetComponent(); 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)); } }