返回介绍

NavMeshAgent.CompleteOffMeshLink 完成分离网格链接

发布于 2019-12-18 15:38:05 字数 4962 浏览 1349 评论 0 收藏 0

JavaScript => public function CompleteOffMeshLink(): void;
C# => public void CompleteOffMeshLink();

Description 描述

Completes the movement on the current OffMeshLink.

在当前分离网格链接上完成运动。

The agent will move to the closest valid navmesh position on the other end of the current OffMeshLink.

在当前的分离网格链接的另一端,代理将移动到最近的有效导航网格位置上。

CompleteOffMeshLink has no effect unless the agent is on an OffMeshLink (See Also: isOnOffMeshLink).

CompleteOffMeshLink 没有效果除非代理在OffMeshLink上(请参考:isOnOffMeshLink)。

When autoTraverseOffMeshLink is disabled an agent will pause at an off-mesh link until this function is called. It is useful for implementing custom movement across OffMeshLinks.

当autoTraverseOffMeshLink 是禁用的,在分离网格链接上代理将会暂停直到该函数被调用。这是用于执行自定义穿过分离网格链接运动的。

JavaScript:

#pragma strict
public enum OffMeshLinkMoveMethod {
	Teleport,
	NormalSpeed,
	Parabola
}
 
@script RequireComponent(NavMeshAgent)
public var method = OffMeshLinkMoveMethod.Parabola;
 
function Start() {
	var agent : NavMeshAgent = GetComponent.<NavMeshAgent>();
	agent.autoTraverseOffMeshLink = false;
	while (true) {
		if (agent.isOnOffMeshLink) {
			if (method == OffMeshLinkMoveMethod.NormalSpeed)
				yield StartCoroutine(NormalSpeed(agent));
			else if (method == OffMeshLinkMoveMethod.Parabola)
				yield StartCoroutine(Parabola(agent, 2.0f, 0.5f));
			agent.CompleteOffMeshLink();
		}
		yield;
	}
}
function NormalSpeed(agent : NavMeshAgent) {
	var data : OffMeshLinkData = agent.currentOffMeshLinkData;
	var endPos : Vector3 = data.endPos + Vector3.up * agent.baseOffset;
	while ( agent.transform.position != endPos ) {
		Debug.Log ("NormalSpeed : " + agent.speed * Time.deltaTime);
		agent.transform.position = Vector3.MoveTowards(agent.transform.position, endPos, agent.speed * Time.deltaTime);
		yield;
	}
}
function Parabola(agent : NavMeshAgent, height : float, duration : float) {
	var data = agent.currentOffMeshLinkData;
	var startPos : Vector3 = agent.transform.position;
	var endPos : Vector3 = data.endPos + Vector3.up * agent.baseOffset;
	var normalizedTime : float = 0.0f;
	while ( normalizedTime < 1.0f ) {
		var yOffset = height * 4.0f * (normalizedTime - normalizedTime * normalizedTime);
		agent.transform.position = Vector3.Lerp(startPos, endPos, normalizedTime) + yOffset * Vector3.up;
		normalizedTime += Time.deltaTime / duration;
		yield;
	}
}

C#:

using UnityEngine;
using System.Collections;
 
public enum OffMeshLinkMoveMethod {
	Teleport,
	NormalSpeed,
	Parabola
}
 
[RequireComponent (typeof (NavMeshAgent))]
public class AgentLinkMover : MonoBehaviour {
	public OffMeshLinkMoveMethod method = OffMeshLinkMoveMethod.Parabola;
	IEnumerator Start () {
		NavMeshAgent agent = GetComponent<NavMeshAgent> ();
		agent.autoTraverseOffMeshLink = false;
		while (true) {
			if (agent.isOnOffMeshLink) {
				if (method == OffMeshLinkMoveMethod.NormalSpeed)
					yield return StartCoroutine (NormalSpeed (agent));
				else if (method == OffMeshLinkMoveMethod.Parabola)
					yield return StartCoroutine (Parabola (agent, 2.0f, 0.5f));
				agent.CompleteOffMeshLink ();
			}
			yield return null;
		}
	}
	IEnumerator NormalSpeed (NavMeshAgent agent) {
		OffMeshLinkData data = agent.currentOffMeshLinkData;
		Vector3 endPos = data.endPos + Vector3.up*agent.baseOffset;
		while (agent.transform.position != endPos) {
			agent.transform.position = Vector3.MoveTowards (agent.transform.position, endPos, agent.speed*Time.deltaTime);
			yield return null;
		}
	}
	IEnumerator Parabola (NavMeshAgent agent, float height, float duration) {
		OffMeshLinkData data = agent.currentOffMeshLinkData;
		Vector3 startPos = agent.transform.position;
		Vector3 endPos = data.endPos + Vector3.up*agent.baseOffset;
		float normalizedTime = 0.0f;
		while (normalizedTime < 1.0f) {
			float yOffset = height * 4.0f*(normalizedTime - normalizedTime*normalizedTime);
			agent.transform.position = Vector3.Lerp (startPos, endPos, normalizedTime) + yOffset * Vector3.up;
			normalizedTime += Time.deltaTime / duration;
			yield return null;
		}
	}
}

navmeshagent

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

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

发布评论

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