博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义OffMeshLink跳跃曲线
阅读量:5247 次
发布时间:2019-06-14

本文共 4399 字,大约阅读时间需要 14 分钟。

 

upload_2015-3-25_13-57-30.png 

很多时候我们需要做类似上图的OffMeshLink的跳跃功能,并方便添加跳跃动画,Unity本身没提供,但是有官方的案例提供了方法,很多人没找到,故贴出来

原链接:https://github.com/Unity-Technologies/NavMeshComponents/blob/master/Assets/Examples/Scripts/AgentLinkMover.cs

我在它的基础上添加了两个多播委托及快速转向(红字显示),用来检测跳跃开始及结束,方便用来做跳跃动画的,而且NavMeshAgentde转速太慢,我添加一个快速转向到落地点的方法。

用不到的就用原版吧。

using UnityEngine;using System.Collections;using UnityEngine.AI;public enum OffMeshLinkMoveMethod{    Teleport,    NormalSpeed,    Parabola,    Curve}[RequireComponent(typeof(NavMeshAgent))]public class AgentLinkMover : MonoBehaviour{    public OffMeshLinkMoveMethod method = OffMeshLinkMoveMethod.Parabola;    public AnimationCurve curve = new AnimationCurve();    public float CurveTime = .5f;    public float FaceTime = .1f;    public delegate void OnStartEvent();    public event OnStartEvent OnStart;    public delegate void OnCompleteEvent ();    public event OnCompleteEvent OnComplete;    IEnumerator Start()    {        NavMeshAgent agent = GetComponent
(); agent.autoTraverseOffMeshLink = false; while (true) { if (agent.isOnOffMeshLink) { yield return StartCoroutine (FaceToTarget (agent, FaceTime)); if (method == OffMeshLinkMoveMethod.NormalSpeed) yield return StartCoroutine(NormalSpeed(agent)); else if (method == OffMeshLinkMoveMethod.Parabola) yield return StartCoroutine(Parabola(agent, 2.0f, 0.5f)); else if (method == OffMeshLinkMoveMethod.Curve) yield return StartCoroutine(Curve(agent, CurveTime)); agent.CompleteOffMeshLink(); agent.updateRotation = true; OnComplete (); } yield return null; } } IEnumerator FaceToTarget(NavMeshAgent agent,float duration){ agent.updateRotation = false; OffMeshLinkData data = agent.currentOffMeshLinkData; Quaternion startRotation = agent.transform.rotation; Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset; var endRotation = Quaternion.LookRotation (new Vector3 (endPos.x - agent.transform.position.x, 0, endPos.z - transform.position.z)); float normalizedTime = 0.0f; while (normalizedTime < 1.0f) { agent.transform.rotation = Quaternion.Slerp(startRotation ,endRotation ,normalizedTime); normalizedTime += Time.deltaTime / duration; yield return null; } OnStart (); } 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 = curve.Evaluate(normalizedTime); agent.transform.position = Vector3.Lerp(startPos, endPos, normalizedTime) + yOffset * Vector3.up; normalizedTime += Time.deltaTime / duration; yield return null; } }}

 

用法,附着到NavMeshAgent物体上,选择需要的移动方式即可,然后搭配一下代码食用更佳:

AgentLinkMover mover = GetComponent
();mover.OnStart += () => print("start");mover.OnComplete += () => print("complete");

 

转载于:https://www.cnblogs.com/CodeSnippet/p/7835361.html

你可能感兴趣的文章
java中静态代码块的用法 static用法详解
查看>>
Java线程面试题
查看>>
Paper Reading: Relation Networks for Object Detection
查看>>
day22 01 初识面向对象----简单的人狗大战小游戏
查看>>
mybatis源代码分析:深入了解mybatis延迟加载机制
查看>>
Flask三剑客
查看>>
Hibernate-缓存
查看>>
【BZOJ4516】生成魔咒(后缀自动机)
查看>>
提高PHP性能的10条建议
查看>>
svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方法...
查看>>
熟用TableView
查看>>
Java大数——a^b + b^a
查看>>
poj 3164 最小树形图(朱刘算法)
查看>>
服务器内存泄露 , 重启后恢复问题解决方案
查看>>
android一些细节问题
查看>>
KDESVN中commit时出现containing working copy admin area is missing错误提示
查看>>
利用AOP写2PC框架(二)
查看>>
【动态规划】skiing
查看>>
java定时器的使用(Timer)
查看>>
ef codefirst VS里修改数据表结构后更新到数据库
查看>>