Rotating a navmesh before moving.

I am working on basic click to move kind of game I have movement working find, but I’m having a minor issue I’m stuck on.
I can not get the navAgent to rotate towards the hit position before moving. Instead it this slow rotation while the navAgent is moving towards the destination.

I tried transform.lookat but instantly rotates to the direction, but I’m trying to get a slow rotation.

Can someone help me with getting the navagent to slowly rotate to the hitpoint before moving?

  void PlayerMovement() {
          //  RaycastHit hit;
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            float remaingdist = navAgent.remainingDistance;
                if (Input.GetMouseButtonDown(0) && navAgent.remainingDistance == 0)
                {
                    if (Physics.Raycast(ray, out hit, 100))
                    {
 
                    //   Debug.Log("The hit point is " + hit.point);
                               
                    navAgent.SetDestination(hit.point);
                    }
                }
           
            //
            if (navAgent.remainingDistance > 0)
                {        
                    Anim.SetFloat("Speed_f", .26f);
                 
                }
            }

Thanks for any help in advance.

Get the direction of the hit point from the agent and have it rotate in that direction using Quaternion.LookRotation. Unity have a good example of how you would do that in the API.

To check whether the agent has finished rotating towards the hit point, use Quaternion.Angle. Once it’s less than 1 (for example) then set the agent destination to the hit point.

1 Like