Enabling a Character to Navigate

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:-


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

Page last updated: 2013-10-04