[Help] - I'm trying to rotate my character to face the mouse position, but it seems to only rotate around the centre of the world (0,0,0)

Character rotation, only happens when mouse is rotated around world centre (0,0,0) -

The MoveToMouse() works perfectly, click on a point in the world, and the player moves, and camera follows.

But when holding shift to rotate the character to where mouse is pointing, it only points relative to the world centre.

transform.LookAt - works perfectly, but want to be able to smooth the rotation.

 using UnityEngine;
 using UnityEngine.AI;
 
 public class ClickToMove : MonoBehaviour
 {
     NavMeshAgent player;
     public float rotSpeed = 10f;
 
     void Start()
     {
         player = GetComponent<NavMeshAgent>();
     }
 
     void Update()
     {
         MoveToMouse();
         LookAtMouse();
     }
 
     void MoveToMouse()
     {
         if (Input.GetMouseButtonDown(1))
         {
             RaycastHit hit;
 
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
             {
                 player.destination = hit.point;
             }
         }
     }
 
     void LookAtMouse()
     {
         if (Input.GetKey(KeyCode.LeftShift))
         {
             RaycastHit lookHit;
 
             Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out lookHit, 100);
              
            //transform.LookAt - works perfectly, but want to be able to smooth the rotation.
            //transform.LookAt(lookHit.point);
 
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookHit.point), rotSpeed * Time.deltaTime);
         }
     }
 }

Privacy & Terms