В настоящее время я использую модель Такси из противника с ИИ, который будет ездить в разные точки пути. Всякий раз, когда автомобиль движется к путевой точке, он автоматически поворачивается на 90 градусов вправо, сразу же, но продолжает движение от путевой точки к путевой точке.
Как исправить агент NavMeshAgent, который автоматически поворачивается на 90 градусов при движении к путевой точке? Закомментированный код исправляет автоповорот, но он недостаточно вращается при перемещении к путевой точке после setDestination.
Код без комментариев сначала поворачивается на 90 градусов, а затем немного поворачивается после каждой путевой точки (из положения на 90 градусов). (Unity API-скрипт из Vector3.RotateTowards)
_agent.UpdateRotation = false, останавливает начальное вращение, но затем мне нужно вручную управлять вращением (с которым мне трудно)
![screenshot](https://i.stack.imgur.com/slr58.png)
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);
}
}
}