I'm trying to rotate my character to face the mouse position, only works at (0,0,0) UNITY

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.

usingUnityEngine;
usingUnityEngine.AI;

publicclassClickToMove:MonoBehaviour {
     NavMeshAgent player;
publicfloatrotSpeed =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),outhit,100))
             {
                 player.destination = hit.point;
             }
         }
     }

void LookAtMouse() {
if(Input.GetKey(KeyCode.LeftShift))
         {
             RaycastHit lookHit;

             Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),outlookHit,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);
         }
     }
 }

Hi @bloatedniknak,

It sounds like you’re looking for help with a specific project hosted on GitHub. While it’s definitely possible another GitHub user might have run into this same issue and can help, the GitHub Community Forum focuses primarily on topics related to GitHub itself or collaboration on project development and ideas.

We want to make sure you’re getting the best support you can, I’d recommend checking  https://unity3d.com/community.

Best of luck!

Andrea

1 Like