返回介绍

NavMesh.CalculatePath 计算路径

发布于 2019-12-18 15:38:04 字数 3957 浏览 1887 评论 0 收藏 0

JavaScript => public static function CalculatePath(sourcePosition: Vector3, targetPosition: Vector3, areaMask: int, path: NavMeshPath): bool;
C# => public static bool CalculatePath(Vector3 sourcePosition, Vector3 targetPosition, int areaMask, NavMeshPath path);

Parameters 参数

sourcePositionThe initial position of the path requested.
路径要求的初始位置。
targetPositionThe final position of the path requested.
路径要求的最终位置。
areaMaskA bitfield mask specifying which NavMesh areas can be passed when calculating a path.
当计算路径,位阈遮罩指定哪一个导航网格区域可以被通过。
pathThe resulting path.
最终路径。

Returns 返回

bool True if a either a complete or partial path is found and false otherwise.
当发现全部或者局部路径时为true,其他则为false。

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.

该函数可以被用于游戏中提前设计路径避免延迟。另一个用法是在移动代理之前检查目标位置是否可到达。

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.

与 NavMeshAgent.SetDestination相比,这是异步调用,该函数立即计算路径。这可以用于长路径并可能在帧率上引起不知名错误。推荐仅用于每帧寻找小路径,例如当评估到替代点的距离。

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.

返回路径可以用于设置代理的路径,使用NavMeshAgent.SetPath。代理需要关闭工作设定的路径的起始点。

JavaScript:

#pragma strict
// ShowGoldenPath.cs
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);
	}
}

C#:

// 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);		
	}
}

navmesh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文