- Home /
NavMeshAgent rotation
In my project I use 2D-Sprite (on a plane) that uses a NavAgent to navigate. Since I use a plane, I do not want the NavMesh to change the gameObjects rotation. However, I still want to get the rotation the NavMesh would theoretically assign, to determine which Animation to use. Does anyone know, how to get this rotation?
Answer by TomH · Jul 10, 2012 at 08:48 AM
If everyone needs it, you can calculate the desired direction. This snippet calculates the angle between 0 and 360:
NavMeshAgent agent = GetComponent<NavMeshAgent>();
float angle = Vector3.Angle(agent.velocity.normalized, this.transform.forward);
if (agent.velocity.normalized.x < this.transform.forward.x)
{
angle *= -1;
}
angle = (angle + 180.0f) % 360.0f;
You may need to adjust some values, depending one the axes you're using. (In my case, left/right was determined by the x-value)
Answer by bibouze · Oct 17, 2012 at 04:52 PM
if you want to stop rotation use the updateRotation field of the NavMeshAgent class
// Drag & Drop your navmesh agent from the inspector
var Nav : NavMeshAgent;
function Update()
{
Nav.destination = target.position;
//This line will stop the rotation of your navmesh
Nav.updateRotation = false;
}
Your answer
Follow this Question
Related Questions
Get NavMeshAgent to look where it's going 0 Answers
Navmesh agent stuck on partial path 0 Answers
GameObject with NavMeshAgent resets rotation when game starts 2 Answers
How to build a custom shape NavMesh Area 1 Answer
Trouble with 2 Navmesh Agents going in circles, but should find eachother 0 Answers