Version: 5.3 (switch to 5.4b)
IdiomaEnglish
  • C#
  • JS

Idioma de script

Selecciona tu lenguaje de programación favorito. Todos los fragmentos de código serán mostrados en este lenguaje.

NavMesh.CalculatePath

Sugiere un cambio

¡Éxito!

Gracias por ayudarnos a mejorar la calidad de la documentación de Unity. A pesar de que no podemos aceptar todas las sugerencias, leemos cada cambio propuesto por nuestros usuarios y actualizaremos los que sean aplicables.

Cerrar

No se puedo enviar

Por alguna razón su cambio sugerido no pudo ser enviado. Por favor <a>intente nuevamente</a> en unos minutos. Gracias por tomarse un tiempo para ayudarnos a mejorar la calidad de la documentación de Unity.

Cerrar

Cancelar

Cambiar al Manual
public static function CalculatePath(sourcePosition: Vector3, targetPosition: Vector3, areaMask: int, path: NavMeshPath): bool;
public static bool CalculatePath(Vector3 sourcePosition, Vector3 targetPosition, int areaMask, NavMeshPath path);

Parámetros

sourcePosition 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.

Valor de retorno

bool True if a either a complete or partial path is found and false otherwise.

Descripción

Calcular la ruta entre dos puntos y almacenar el resultado.

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.


        
// ShowGoldenPath.cs
using UnityEngine;
using System.Collections;
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);		
	}
}