0

I have enemies that patrol to different waypoints using NavMesh Agent I want when the enemy reach the next waypoint to have the same rotation as that waypoint. Here is the code:

void Update ()
{
    if (agent.remainingDistance < 0.1)
        {
            // tried to stop the agent so I can override it's rotation, doesn't work
            agent.Stop();
            // Give him the desired rotation
            transform.rotation = wayPoints[curretPoint].rotation;

            if (curretPoint < wayPoints.Length -1)
            {
                curretPoint++;
            }
            else 
            {
                curretPoint = 0;
            }
            // make him wait for a fixed amount of time
            patrolTimer += Time.deltaTime;
            if (patrolTimer >= patrolWait)
            {
                patrolTimer = 0;
                agent.SetDestination (wayPoints[curretPoint].position);
                agent.Resume ();
            }
        }
}

The problem is that he keeps rotating back and forth very quickly, I can't get teh desired effect that I want.

Abdou023
  • 1,654
  • 2
  • 24
  • 45

2 Answers2

0

Try setting Angular Speed of NavMesh Agent to 0.

Edit:

That should work:

    // make him wait for a fixed amount of time
    patrolTimer += Time.deltaTime;
    if (patrolTimer >= patrolWait)
    {
        if (curretPoint < wayPoints.Length -1)
        {
            curretPoint++;
        }
        else 
        {
             curretPoint = 0;
        }
        patrolTimer = 0;
        agent.SetDestination (wayPoints[curretPoint].position);
        agent.Resume ();
    }
Woltus
  • 438
  • 4
  • 14
0

This is how I handled it: Instead of doing agent.Stop(); and agent.Resume(); I simply set its speed to 0 and used transform.Rotate to rotate the character.

Dávid Florek
  • 552
  • 6
  • 17