Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

NavMeshAgent.CompleteOffMeshLink

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public method CompleteOffMeshLink(): void;
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).

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.

#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; } }
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; } } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答