Version: 2019.1
public void CompleteOffMeshLink ();

説明

現在の OffMeshLink 上の移動を完了します。

エージェントは現在の OffMeshLink のもう一方の端の最も近い有効な NavMesh の位置に移動します。

エージェントが OffMeshLink 上でなければ CompleteOffMeshLink は影響しません (参照: 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.

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; } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961