Search code examples
c#unity-game-enginerotationquaternionsgame-development

Unity Fix NavMeshAgent Auto Rotate 90 degrees


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.

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.

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 , stops initial rotation but I then have to control rotation manually (which I am having a hard time with)

screenshot

 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);
                        }
                    }
                }


Solution

  • 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

    Or

    Change the direction of the model in blender or any other 3d modeling tool