Unable to stop navmeshagent for rotation before moving

I'm trying to get the NavMeshAgent to rotate before calculating a path and proceed walking.

I stumbled upon a thread that said this can be done by manually rotating the object first.

However, I am still stumped as to how to do this as my unit either a)refuses to move or b)moves, but does not stop to rotate.

Below is the code I've attempted.

void Update() {

RaycastHit hit;
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.collider.tag == "Ground")
{
targetPosition = hit.point;
}
}

if (transform.rotation != _lookRotation) //check if unit is in line with direction of hit.point
{
agent.Stop(); //stops the navmeshagent from moving.

_direction = (targetPosition - transform.position).normalized;
_lookRotation = Quaternion.LookRotation(_direction);

transform.rotation = Quaternion.RotateTowards(transform.rotation, _lookRotation, Time.deltaTime * speed);

} //perform rotation
else //else, if already in line, set path and walk.
{

agent.SetDestination(targetPosition);

}


1.) You need to update _lookRotation when the mouse is clicked or it never changes so your agent will never look at a new location.

Simple Coroutine Example:

IEnumerator RotateAgent(Quaternion currentRotation, Quaternion targetRotation) {

IsRotating = true;
while(current rotation != targetRotation) {
transform.rotation = Quaternion.RotateTowards(currentRotation, targetRotation, RotationSpeed * Time.deltaTime);

yield return 1;
}
IsRotating = false;
}


Then on Mouse Click:

StartCoroutine(RotateAgent(transform.rotation, _lookRotation);


And only do Agent.SetDestination if IsRotating = false;

• In your second point, is there a way to get a definite "==" angle occur? At the end of the day, I'm just hoping to have the rotation happen over a constant speed and finally moving when it reaches said angle. – user4985 Jun 8 '15 at 10:25
• The best way would be a coroutine thats called on mouse click that will rotate the agent over a set time. Have it set a flag you check in the Update, if the flags not set then no rotation is going on and the agent can move. – Jeremiah Leslie Jun 8 '15 at 10:32
• StartCoroutine - link – Jeremiah Leslie Jun 8 '15 at 10:34
• Hi, I managed to do it by merging your code with a path reset function on the NavMeshAgent. Appreciate the help. – user4985 Jun 9 '15 at 10:40