My journey becoming a Unity game developer: Enemy AI-Setup the guards to patrol the area (Part 5)
Objective: Making the guards go from an Idle animation at the 1st and last waypoints to the Walk animation through the other waypoints in their path.
Let’s begin in the Animations folder by creating a new Animator Controller inside of the Guard folder, and name it Guard_controller. In the Hierarchy, select all 3 Security Guards game objects and create new Animator components for each of them. In the Animator component, drag the Guard_controller into the Controller slot for each of them, so we can now add the animations the guards will use on their patrols.
Open up the Animator, then drag the Idle and WalkingAlerted animations onto the layout area. Right-click on these animations and select Make Transitions to each of them. Over at the Parameters tab, add a new Parameter Type Bool and either name it Patrolling or something else to your liking.
Click on the Idle->WalkingAlerted transition arrow, then uncheck the Has Exit Time so we don’t have to wait for the animations to play in its entirety when we need change over to the other animation. Down below in the Conditions section, add a new condition with Patrolling or whatever name you chose for this Bool condition, and set it to True. This condition will make it change from the Idle animation to the Walking animation.
For the WalkingAlerted->Idle transition, uncheck the Has Exit Time as well, but for the new Bool condition we want to set the Patrolling to False so the Walking animation changes back to the Idle animation.
Now when we press Play, we can see the guards Idle and Walk animations play by turning on and off the Patrolling parameter in the Animator.
Next, we want to control the guards animations through code instead. Create a new variable Type Animator name anim which will be the handle for the Animator component. Then inside the Start() function, we want to get the Animator component with the anim variable.
Now inside of the Update() function we want to check IF the distance is less than 1 unit, and the current target waypoint is either the 1st waypoint OR the last waypoint. If the conditions are met, NULL check if the Animator component is valid. If it is, set the Patrolling parameter to False so the Idle animation plays. If it’s not, NULL check one more time for the Animator, then set the Patrolling parameter to True which will have the guards walking through the waypoints in their paths.
When we press Play, the guards Walk animation plays as they go through the waypoints and the Idle animation plays when they reach the 1st or last waypoint.
One last thing we did was add a new waypoint to the 2nd Security Guard’s path and removed one from the 3rd guard’s path. Also, we changed the position of the 2nd and 3rd guards starting point in their paths as well. Now the 2nd guard will have 4 waypoints to walk to and the 3rd guard will have only 2 waypoints in his path. The 2nd guard’s path we will use code to make him pick between the 2nd or 3rd waypoint to go to in the future.
Now we will see the guards moving through their new paths on their patrols.
Next time we’re going to work on the guards being able to see by giving them eye functionality.