Select your preferred scripting language. All code snippets will be displayed in this language.
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.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
ClosesourcePosition | The initial position of the path requested. |
targetPosition | The final position of the path requested. |
areaMask | A bitfield mask specifying which NavMesh areas can be passed when calculating a path. |
path | The resulting path. |
bool True if a either a complete or partial path is found and false otherwise.
Calculate a path between two points and store the resulting path.
This function can be used to plan a path ahead of time to avoid a delay in gameplay when the path is needed. Another use is to check if a target position is reachable before moving the agent.
In contrast to NavMeshAgent.SetDestination, which is asyncronous call, this function calculates the path immeditely. This can be costly operation for very long paths and can cause hiccup in the frame rate. It is recommended to do only a few path finds per frame, for example when evaluating distances to cover points.
The returned path can be used to set the path for an agent using NavMeshAgent.SetPath. The agent needs to be close the starting point for the set path to work.
#pragma strict // ShowGoldenPath public class ShowGoldenPath extends MonoBehaviour { public var target: Transform; private var path: NavMeshPath; private var elapsed: float = 0.0f; function Start() { path = new NavMeshPath(); elapsed = 0.0f; } function Update() { // Update the way to the goal every second. elapsed += Time.deltaTime; if (elapsed > 1.0f) { elapsed -= 1.0f; NavMesh.CalculatePath(transform.position, target.position, NavMesh.AllAreas, path); } for (var i: int = 0; i < path.corners.Length - 1; i++) Debug.DrawLine(path.corners[i], path.corners[i + 1], Color.red); } }
// ShowGoldenPath using UnityEngine; using UnityEngine.AI;
public class ShowGoldenPath : MonoBehaviour { public Transform target; private NavMeshPath path; private float elapsed = 0.0f; void Start() { path = new NavMeshPath(); elapsed = 0.0f; }
void Update() { // Update the way to the goal every second. elapsed += Time.deltaTime; if (elapsed > 1.0f) { elapsed -= 1.0f; NavMesh.CalculatePath(transform.position, target.position, NavMesh.AllAreas, path); } for (int i = 0; i < path.corners.Length - 1; i++) Debug.DrawLine(path.corners[i], path.corners[i + 1], Color.red); } }
sourcePosition | The initial position of the path requested. |
targetPosition | The final position of the path requested. |
filter | A filter specifying the cost of NavMesh areas that can be passed when calculating a path. |
path | The resulting path. |
bool True if a either a complete or partial path is found and false otherwise.
Calculates a path between two positions mapped to the NavMesh, subject to the constraints and costs defined by the filter argument.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!