Legacy Documentation: Version 4.6
Language: English
Navigation Meshes
Off-mesh Links

Enabling a Character to Navigate

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Once the navmesh is set up, the characters need to be equipped to use it. The Nav Mesh Agent component handles both the pathfinding and the movement control of a character. In your scripts, navigation can be as simple as setting the desired destination point - the Nav Mesh Agent can handle everything from there on.

You can add a Nav Mesh Agent component either from a script or from the editor (menu: Component -> Navigation -> Nav Mesh Agent). The component has a number of properties that are explained fully in the Nav Mesh Agent component reference page, but the default values usually work well as a base for experimentation.

In your script, you will need to get a reference to the NavMeshAgent component since there is no property to access it directly:

var agent: NavMeshAgent = GetComponent.<NavMeshAgent>();

Then, to set the character in motion, you just need a single call to SetDestination:

agent.SetDestination(targetPoint);

The target point is specified as a point in world space; the navigation system will find the closest point on the navmesh automatically, so it doesn’t matter if the target point is not precisely on the mesh surface.

Naturally, the navigation system can’t handle every aspect of the game logic, so you need to have some method of choosing the appropriate destination point in the first place. This task depends heavily on the nature of the gameplay but some common situations include:

  • Moving an NPC to a player’s position to start an attack;
  • Sending a character to a preset target marked by an empty GameObject (e.g. an exit from a building or the location of a treasure);
  • Moving to a map location clicked with the mouse.
Navigation Meshes
Off-mesh Links