Patrolling Enemies

Read Time:1 Minute, 43 Second

Root Motion vs Nav Mesh Agents

I did a deepish dive into this topic on Zombie Runner and forgot about it when I wrote the patrol code for Bossk. He was hanging up on the corners and I suspect it was because the agent was moving faster than he was or something like that.

My solution in Zombie Runner and here as well is to shut down the Root Motion and allow the Nav Mesh Agent to drive Bossk about but then to set the speed of the Agent using the Root Motion animation. It looks like this:

Decouple root motion on the Animator and let the Agent drive the character,
but update the Agent speed whenever the Animator would have moved the character.

Mover Component

This component deals with starting and stopping the Nav Mesh Agent and Animator components whenever we want the character AI to move to some part of the level.


Waypoint Handler

This script handles the Patrol action of the Bossk character in the video. It requires a game object set up with children that have colliders set up as triggers. When the trigger is flipped it checks to see if the collider belongs to the current waypoint and if it does then we move on to the next waypoint.


Enemy Animator Controller

Part of this code was discussed earlier, but I wanted to show the whole thing here because it shows my strategy for dealing with animator properties by wrapping the string literals with constants and using properties to encapsulate the animator Get and Set calls.

You can go a level deeper by using the String To Hash method of the Animator object. It is a static method that retrieves the hash ID of whatever property has the given name. Then you supply the hash ID instead of the string to the Get and Set methods. Since the search will be done on an integer instead of a string, the result is a faster seek for the property.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleppy
Sleppy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *