I added a throttle to the Movement Animator so that characters can be set to walk or run. In the interest of keeping the Animator complete separate from the Agent and AI code, Mover also received two new methods called Walk and Run which invoke events of the same name exposed to the inspector:
These controls can be used before Mover.GoTo calls to set the maximum speed of the Nav Mesh Agent according to the speed settings in the Movement Animator component (or whatever custom component is put in its place.
Added a Restart method to the Action base class. Restart is called by the Brain when an Action is removed from the stack and set as the current Action.
The Restart method was added specifically because the Patrol Action needed it in order to resume moving to the correct Waypoint. The previous behavior would send the character to the next Waypoint rather than the current one.
I added a component called Waypoint which sits on the Game Objects in a Waypoint Path. The Waypoint has a reference to an Action that the patrolling character will execute when that waypoint is reached.
Patrol checks if the Mover has reached its destination. When it does and if the Waypoint has an Action set, then it calls Set Action on the Brain and sets the next Waypoint to be the current Waypoint. Go To Waypoint is not called until Restart is called by the Brain.
If no Action is set then Patrol calls Go To Waypoint.
I want to stop a character from moving or rotating while an attack is being executed. The first step is to extract the rotation code from the Attack code. I moved the rotation code to the Focus component. Rotation will be handled by the Attack Target component.
Let’s talk about the Attack Target sequence.
- Execute has multiple guards to bail should the Attacker already be Attacking or if the Target is null, dead, or the Attacker.
- The range between the Attacker and the Target is calculated. If the Target is in range then the Mover is set to Idle and the angle to the Target is calculated. If the angle is inside the Attack arc then the Attacker executes the current Attack. Otherwise, the Focus rotates the Attacker to face the Target.
- If the range to the Target exceeds the chase range and the attack has a chase Action then that Action replaces the attack Action. Otherwise, the attack Action is cleared.
- If the range to the Target is inside the chase range then the Attacker moves toward the Target at a run.