Unity Fix NavMeshAgent自动旋转90度 [英] Unity Fix NavMeshAgent Auto Rotate 90 degrees
问题描述
我目前正使用AI的敌人作为出租车模型,该模型会驶向不同的航路点.每当汽车移动到某个路点时,它都会立即向右自动旋转90度,但会不断在一个路点之间移动.
I am currently using a Taxi model from as an enemy with AI that will drive to different waypoints. Whenever the car moves to a waypoint it auto rotates 90 degrees to the right, right away but keeps moving from waypoint to waypoint.
如何修复在移动到航路点时自动旋转90度的NavMeshAgent?注释掉的代码可修复自动旋转,但在setDestination上移动到航路点时旋转不足.
How do I fix a NavMeshAgent that auto turns 90 degrees when moving to a waypoint? The commented out code fixes the auto rotate but does not rotate enough when moving to the waypoint upon setDestination.
未注释的代码首先旋转90度,然后在每个航路点之后(从90度位置开始)旋转一点.(来自Vector3.RotateTowards的Unity API脚本)
The uncommented code first rotates 90 degrees and then rotates a little after each waypoint (from the 90 degree position). (Unity API script from Vector3.RotateTowards)
_agent.UpdateRotation = false,停止初始旋转,但是我必须手动控制旋转(我很难过)
_agent.UpdateRotation = false , stops initial rotation but I then have to control rotation manually (which I am having a hard time with)
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°偏移来进行更改.之后,您可以使用父对象进行移动和旋转
This is due to the fact that the taxi model itself has an 90° offset. You can change that with creating a prefab for the taxi model with an empty gameobject as parent and give the taxi the needed 90° offset on the prefab. After that you use the parent object for movement and rotation
或
在Blender或任何其他3d建模工具中更改模型的方向
Change the direction of the model in blender or any other 3d modeling tool
这篇关于Unity Fix NavMeshAgent自动旋转90度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!