Legacy Documentation: Version 5.4
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

NavMeshAgent.nextPosition

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

Switch to Manual
public var nextPosition: Vector3;
public Vector3 nextPosition;

Description

Gets or sets the simulation position of the navmesh agent.

The position vector is in world space coordinates and units.

The nextPosition is coupled to Transform.position. In the default case the navmesh agent's Transform position will match the internal simulation position at the time the script Update function is called. This coupling can be turned on and off by setting updatePosition.

When updatePosition is true, the Transform.position reflects the simulated position, when false the position of the transform and the navmesh agent is not synchronized, and you'll see a difference between the two in general. When updatePosition is turned back on, the Transform.position will be immediately move to match nextPosition.

By setting nextPosition you can directly control where the internal agent position should be. The agent will be moved towards the position, but is constrained by the navmesh connectivity and boundaries. As such it will be useful only if the positions are continuously updated and assessed. See Also: Warp for teleporting a navmesh agent.

#pragma strict
function Start() {
	// Update the transform position explicitly in the OnAnimatorMove callback
	GetComponent.<NavMeshAgent>().updatePosition = false;
}
function OnAnimatorMove() {
	transform.position = GetComponent.<NavMeshAgent>().nextPosition;
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Start () { // Update the transform position explicitly in the OnAnimatorMove callback GetComponent<NavMeshAgent>().updatePosition = false; } void OnAnimatorMove () { transform.position = GetComponent<NavMeshAgent>().nextPosition; } }

Additionally it can be useful to control the agent position directly - especially if the GO transform is controlled by something else - e.g. animator, physics, scripted or input.

#pragma strict
public var agentIsControlledByOther: boolean;
function Update() {
	var agent: NavMeshAgent = GetComponent.<NavMeshAgent>();
	agent.updatePosition = !agentIsControlledByOther;
	if (agentIsControlledByOther) {
		GetComponent.<NavMeshAgent>().nextPosition = transform.position;
	}
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public bool agentIsControlledByOther; void Update () { NavMeshAgent agent = GetComponent<NavMeshAgent> (); agent.updatePosition = !agentIsControlledByOther; if (agentIsControlledByOther) { GetComponent<NavMeshAgent>().nextPosition = transform.position; } } }

See Also: Move for moving the agent with a relative position.