NavMeshAgent.CompleteOffMeshLink

Switch to Manual
public void CompleteOffMeshLink ();

Description

完成在当前 OffMeshLink 上的移动。

代理将移动到当前 OffMeshLink 另一端上最近的有效导航网格位置。

除非代理在 OffMeshLink 上,否则 CompleteOffMeshLink 将不起作用(另请参阅:isOnOffMeshLink)。

autoTraverseOffMeshLink 被禁用时,代理会在网格外链接处暂停,直到此函数被调用为止。 此函数可用于跨越 OffMeshLink 实现自定义移动。

using UnityEngine;
using UnityEngine.AI;
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; } } }