Legacy Documentation: Version 2018.2 (Go to current version)
Navigation How-Tos
Moving an Agent to a Position Clicked by the Mouse
Other Versions

Telling a NavMeshAgent to Move to a Destination

You can tell an agent to start calculating a path simply by setting the NavMeshAgent.destination property with the point you want the agent to move to. As soon as the calculation is finished, the agent will automatically move along the path until it reaches its destination. The following code implements a simple class that uses a GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary
to mark the target point which gets assigned to the destination property in the Start function. Note that the script assumes you have already added and configured the NavMeshAgent component from the editor.

    // MoveDestination.cs
    using UnityEngine;
    
    public class MoveDestination : MonoBehaviour {
       
       public Transform goal;
       
       void Start () {
          NavMeshAgent agent = GetComponent<NavMeshAgent>();
          agent.destination = goal.position; 
       }
    }
    // MoveDestination.js
    var goal: Transform;

    function Start() {
      var agent: NavMeshAgent = GetComponent.<NavMeshAgent>();
      agent.destination = goal.position; 
    }

Did you find this page useful? Please give it a rating:

Navigation How-Tos
Moving an Agent to a Position Clicked by the Mouse