Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

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

NavMesh.CalculatePath

static function CalculatePath(sourcePosition: Vector3, targetPosition: Vector3, passableMask: int, path: NavMeshPath): bool;
static bool CalculatePath(Vector3 sourcePosition, Vector3 targetPosition, int passableMask, NavMeshPath path);
static def CalculatePath(sourcePosition as Vector3, targetPosition as Vector3, passableMask as int, path as NavMeshPath) as bool

Parameters

sourcePositionThe initial postion of the path requested.
targetPositionThe final position of the path requested.
passableMaskA mask specifying which NavMesh layers can be passed when calculating a path.
pathThe resulting path.

Returns

bool True if a path is found.

Description

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.

	var mesh: NavMesh;
	var target: Transform;

private var path: NavMeshPath;

function Start () { mesh.CalculatePath(transform.position, target.position, -1, path); }

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public NavMesh mesh;
    public Transform target;
    private NavMeshPath path;
    void Start() {
        mesh.CalculatePath(transform.position, target.position, -1, path);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public mesh as NavMesh

	public target as Transform

	private path as NavMeshPath

	def Start() as void:
		mesh.CalculatePath(transform.position, target.position, -1, path)