Unity3D OffNavMesh跳转问题

avu*_*ess 3 navigation curve mesh unity-game-engine

我已经设置了Unity导航网格(四个平面),导航代理(球体)并设置了自动和手动关闭网格链接.它现在应该在网格之间跳转.它确实在网格之间跳跃,但它是直线的.

换句话说,当代理到达边缘时,而不是实际上跳起来(比如关闭网格链接)它只是直线移动但速度稍快.我试着将一架飞机比其他飞机高,但球体仍然是直线跳跃.

它应该是这样的吗?是否可以将导航设置为按某种曲线跳转?或者我应该尝试自己实现?

Mat*_*ond 12

我来自这个问题,不得不深入挖掘Unity样本.我只希望通过提取重要位来让人们更容易.

要在navmesh链接中应用您自己的动画/转换,您需要告诉Unity您将处理所有offmesh链接遍历,然后添加定期检查以查看代理是否在offmesh链接上的代码.最后,当转换完成后,您需要告诉Unity您已移动代理,并恢复正常的navmesh行为.

处理链接逻辑的方式取决于您.你可以直线,有一个旋转的虫洞,无论如何.对于跳转,unity使用动画进度作为lerp参数遍历链接,这非常有效.(如果你正在进行循环或更复杂的动画,这不能很好地工作)

重要的统一位是:

_navAgent.autoTraverseOffMeshLink = false; //in Start()
_navAgent.currentOffMeshLinkData; //the link data - this contains start and end points, etc
_navAgent.CompleteOffMeshLink(); //Tell unity we have traversed the link (do this when you've moved the transform to the end point)
_navAgent.Resume(); //Resume normal navmesh behaviour
Run Code Online (Sandbox Code Playgroud)

现在简单的跳转样本......

using UnityEngine;

[RequireComponent(typeof(NavMeshAgent))]
public class NavMeshAnimator : MonoBehaviour
{
    private NavMeshAgent _navAgent;
    private bool _traversingLink;
    private OffMeshLinkData _currLink;

    void Start()
    {
        // Cache the nav agent and tell unity we will handle link traversal
        _navAgent = GetComponent<NavMeshAgent>();
        _navAgent.autoTraverseOffMeshLink = false;
    }

    void Update()
    {
        //don't do anything if the navagent is disabled
        if (!_navAgent.enabled) return;

        if (_navAgent.isOnOffMeshLink)
        {
            if (!_traversingLink)
            {
                //This is done only once. The animation's progress will determine link traversal.
                animation.CrossFade("Jump", 0.1f, PlayMode.StopAll);
                //cache current link
                _currLink = _navAgent.currentOffMeshLinkData;
                //start traversing
                _traversingLink = true;
            }

            //lerp from link start to link end in time to animation
            var tlerp = animation["Jump"].normalizedTime;
            //straight line from startlink to endlink
            var newPos = Vector3.Lerp(_currLink.startPos, _currLink.endPos, tlerp);
            //add the 'hop'
            newPos.y += 2f * Mathf.Sin(Mathf.PI * tlerp);
            //Update transform position
            transform.position = newPos;

            // when the animation is stopped, we've reached the other side. Don't use looping animations with this control setup
            if (!animation.isPlaying)
            {
                //make sure the player is right on the end link
                transform.position = _currLink.endPos;
                //internal logic reset
                _traversingLink = false;
                //Tell unity we have traversed the link
                _navAgent.CompleteOffMeshLink();
                //Resume normal navmesh behaviour
                _navAgent.Resume();
            }
        }
        else
        {
            //...update walk/idle animations appropriately ...etc
Run Code Online (Sandbox Code Playgroud)