fix lookat camera
This commit is contained in:
@@ -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 hasControl = true; // set this to false if we want to stop reading player inputs
|
||||||
private bool isSprinting = false;
|
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
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
|
groundPlane = new Plane(Vector3.up, -DougBody.transform.position.y);
|
||||||
characterController = GetComponent<CharacterController>();
|
characterController = GetComponent<CharacterController>();
|
||||||
cameraController = GetComponent<CameraController>();
|
cameraController = GetComponent<CameraController>();
|
||||||
SwitchTools(GameManager.Instance.PlayerManager.CurrentToolIndex);
|
SwitchTools(GameManager.Instance.PlayerManager.CurrentToolIndex);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
moveDir = Vector3.zero;
|
moveDir = Vector3.zero;
|
||||||
|
groundPlane.distance = -DougBody.transform.position.y;
|
||||||
|
|
||||||
if (hasControl)
|
if (hasControl)
|
||||||
{
|
{
|
||||||
@@ -201,17 +208,12 @@ void ToolUseDetector()
|
|||||||
void RotatePlayerTowardMouse()
|
void RotatePlayerTowardMouse()
|
||||||
{
|
{
|
||||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
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;
|
Vector3 hitPoint = ray.GetPoint(distance);
|
||||||
targetPosition.y = transform.position.y;
|
|
||||||
|
|
||||||
Vector3 direction = targetPosition - transform.position;
|
DougBody.transform.LookAt(new Vector3(hitPoint.x, DougBody.transform.position.y, hitPoint.z));
|
||||||
Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);
|
|
||||||
|
|
||||||
DougBody.transform.rotation = targetRotation;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user