unity Fix NavMeshAgent 自动旋转90度
Unity Fix NavMeshAgent Auto Rotate 90 degrees
我目前正在使用一个出租车模型作为一个带有 AI 的敌人,它会开到不同的 waypoints。每当汽车移动到一个路点时,它会立即向右自动旋转 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 建模工具中更改模型的方向
我目前正在使用一个出租车模型作为一个带有 AI 的敌人,它会开到不同的 waypoints。每当汽车移动到一个路点时,它会立即向右自动旋转 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 建模工具中更改模型的方向