1.首先在pack manager導入 AI Navigation包
2.參照官方文檔的連接制作跳躍點
https://docs.unity3d.com/Packages/com.unity.ai.navigation@1.1/manual/CreateOffMeshLink.html
注意: 上面的連接跳躍點使用的是Off Mesh Link(Nav Mesh Link)本質上差不太多,自己研究研究就好歉胶,懶得贅述了~
當我們按照文檔操作以后會發(fā)現俺亮,跳躍點其實是走過去的炊汹,不是跳過去的沼本!效果如下
如果要解決這個問題,其實需要額外的代碼來處理跪削,這里官方案例是有的爷速,所以我們導入官方案例。
這里可以看到我的導入按鈕是Reimport啃洋,是因為我已經導入了传货!
導入后,里面有很多案例可以研究宏娄,具體就自己研究了问裕,要解決我們上面的問題,我們需要使用到官方案例的一個腳本孵坚,腳本名字叫“AgentLinkMover”粮宛,所以直接搜索腳本名字,然后掛載到你的Nav Mesh Agent組件的游戲物體上即可卖宠。
如圖:
然后我們運行就會發(fā)現巍杈,可以起跳啦!
大工告成扛伍!
參考連接:“https://www.youtube.com/watch?v=dpJUc_BpChw&t=349s”
腳本代碼:
using UnityEngine.AI;
using System.Collections;
namespace Unity.AI.Navigation.Samples
{
public enum OffMeshLinkMoveMethod
{
Teleport,
NormalSpeed,
Parabola,
Curve
}
/// <summary>
/// Move an agent when traversing a OffMeshLink given specific animated methods
/// </summary>
[RequireComponent(typeof(NavMeshAgent))]
public class AgentLinkMover : MonoBehaviour
{
public OffMeshLinkMoveMethod m_Method = OffMeshLinkMoveMethod.Parabola;
public AnimationCurve m_Curve = new AnimationCurve();
IEnumerator Start()
{
NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.autoTraverseOffMeshLink = false;
while (true)
{
if (agent.isOnOffMeshLink)
{
if (m_Method == OffMeshLinkMoveMethod.NormalSpeed)
yield return StartCoroutine(NormalSpeed(agent));
else if (m_Method == OffMeshLinkMoveMethod.Parabola)
yield return StartCoroutine(Parabola(agent, 2.0f, 0.5f));
else if (m_Method == OffMeshLinkMoveMethod.Curve)
yield return StartCoroutine(Curve(agent, 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;
}
}
IEnumerator Curve(NavMeshAgent agent, 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 = m_Curve.Evaluate(normalizedTime);
agent.transform.position = Vector3.Lerp(startPos, endPos, normalizedTime) + yOffset * Vector3.up;
normalizedTime += Time.deltaTime / duration;
yield return null;
}
}
}
}