我正在尝试为GameObjects创建简单的移动AI。
每个GameObject都有NavMeshAgent
Vector3 destination = new Vector3(Random.Range(-walkArea, walkArea), 0, Random.Range(-walkArea, walkArea));
navAgent.SetDestination(destination);
这就是我想要做的。但我烤制的地面不平坦,可能有30-40个Y轴。
所以如果GameObject周围有山,他就会被困住,不能爬过去。
我能做些什么呢?如果我只使用navAgent.Move(destination)
,一切都会正常工作。GameObject在X-Z位置上传送,不用担心Y轴。
我如何使用SetDestination
做同样的事情
发布于 2020-06-30 22:29:14
我找到了解决方案。
在main GameObject
中,我使用NavMeshAgent
创建了empty gameobject
。
Vector3 destination = new Vector3(Random.Range(-walkArea, walkArea), 0, Random.Range(-walkArea, walkArea));
navDestinationObject.Move(destination);
navAgent.destination = navDestinationObject.transform.position;
navDestinationObject
在.Move
上获得正确的Y轴,然后我们只需将主GameObject
移动到navDestinationObject
位置。
但我认为一定有更好的解决方案。
发布于 2021-05-17 23:31:59
通过强制转换Ray
来获取地面y
,其中groundLayerMask
是地面的LayerMask
,以防止行为不端。
public void WalkTo(Vector3 position)
{
Physics.Raycast(position + Vector3.up * 64, Vector3.down, out RaycastHit hit, 128, groundLayerMask);
position.y = hit.y;
navAgent.SetDestination(position);
}
因此,我们从该位置上方的64个单位投射光线,以找到地面y,然后手动设置它。( RaycastHit是结构体,因此如果没有命中,y将为0。)
https://stackoverflow.com/questions/62658807
复制相似问题