[英]Unity Fix NavMeshAgent Auto Rotate 90 degrees
我目前正在使用出租车模型作为 AI 的敌人,它将驾驶到不同的航点。 每当汽车移动到一个航点时,它会立即自动向右旋转 90 度,但会继续从一个航点移动到另一个航点。
如何修复移动到航点时自动旋转 90 度的 NavMeshAgent? 注释掉的代码修复了自动旋转,但在 setDestination 上移动到航点时旋转不够。
未注释的代码首先旋转 90 度,然后在每个航点之后旋转一点(从 90 度位置)。 (来自 Vector3.RotateTowards 的 Unity API 脚本)
_agent.UpdateRotation = false ,停止初始旋转,但我必须手动控制旋转(我很难使用)
private void Start()
{
_agent = GetComponent<NavMeshAgent>();
// Almost works doesn't rotate enough
//_agent.updateRotation = false;
_isStopped = false;
}
private void Update()
{
//Almost works, doesn't rotate enough
//transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, (_angleToRotate) * 8, 0), 1f);
//Rotates but turns 90 degrees first
Vector3 targetDirection = _wayPoints[_currentWayPoint].transform.position - transform.position;
float singleStep = _speed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
Debug.DrawRay(transform.position, newDirection, Color.red);
transform.rotation = Quaternion.LookRotation(newDirection);
switch (_currentState)
{
case AIState.NonAlert:
//Debug.Log("Not Alert...");
if (_isStopped == true)
{
return;
}
else
{
if (_wayPoints.Count > 0)
{
_agent.SetDestination(_wayPoints[_currentWayPoint].transform.position);
//Gets distance between two Vector3s
float distanceToWayPoint = Vector3.Distance(_wayPoints[_currentWayPoint].transform.position, transform.position);
if (distanceToWayPoint < 1.0f)
{
_currentWayPoint++;
//Almost works, doesnt rotate enough
//_angleToRotate = Vector3.SignedAngle(transform.position, _wayPoints[_currentWayPoint].transform.position, Vector3.up);
}
}
}
这是因为出租车模型本身具有 90° 偏移。 您可以通过为出租车模型创建一个预制件来改变它,将一个空游戏对象作为父项,并在预制件上为出租车提供所需的 90° 偏移。 之后,您使用父对象进行移动和旋转
或者
在搅拌机或任何其他 3d 建模工具中更改模型的方向
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.