版本:Unity 6(6000.0)
语言英语
  • C#

NavMeshAgent.CompleteOffMeshLink

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们不可能接受所有的提交,但我们会阅读用户建议的每项改进,并在适当的情况下进行更新。

关闭

提交失败

由于某种原因,您建议的更改无法提交。请

关闭

取消

声明

public void CompleteOffMeshLink();

说明

完成当前 OffMeshLink 上的移动。

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

CompleteOffMeshLink 在代理处于 OffMeshLink 上时才会生效(其他资源:isOnOffMeshLink)。

autoTraverseOffMeshLink 禁用时,代理会暂停在非网格导航链接上,直到调用此功能。这可用于在非网格导航链接上实现自定义移动。

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