gpt4 book ai didi

unity-game-engine - Unity3D OffNavMesh跳转问题

转载 作者:行者123 更新时间:2023-12-02 13:28:58 29 4
gpt4 key购买 nike

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

换句话说,当智能体到达边缘时,它不会真正跳起来(就像绘制脱离网格链接一样),它只是直线移动,但速度更快一些。我尝试将一架飞机移得比其他飞机更高,但球体仍然沿直线跳跃。

事情应该是这样的吗?是否可以设置导航以跳过某个弯道?或者我应该尝试自己实现它?

最佳答案

我遇到了这个问题,并且不得不深入研究 Unity 示例。我只是希望通过提取重要的部分让人们更容易。

要在导航网格链接上应用您自己的动画/过渡,您需要告诉 Unity 您将处理所有离线网格链接遍历,然后添加定期检查代理是否位于离线网格链接上的代码。最后,当转换完成时,您需要告诉 Unity 您已移动代理,并恢复正常的导航网格行为。

处理链接逻辑的方式取决于您。你可以走一条直线,有一个旋转的虫洞,等等。对于跳跃,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

现在是一个简单的跳跃示例...

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

关于unity-game-engine - Unity3D OffNavMesh跳转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12247647/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com