The Great Fleece: Guard Animations
In the last article I walked through how to set up the AI for our guards. So now they are moving back and forth between their respective waypoints. Animating this was fairly simple.
The goal here was to switch between the idle animation and the walk animation. When the guard reaches their currentTarget they stop for 2 seconds and then resume.
In the project view we have our guard animations.
Right clicked that folder and navigate to create > animator controller. This makes a blank animator controller! Now just simply drag the Idle and the WalkingAlerted animations into the animator view. Set the Idle one to the default.
I created transitions to and from WalkingAlerted. I set up a Bool parameter called “Walk”. In the inspector or the transition I set the conditions accordingly and unchecked “has exit time”.
Now that all that is set up, lets check out the code.
Obviously we need a handle to our animator.
Make sure the Security Guard game object has an animator component and the controller for that component is the controller we just made.
Now all we have to do is use the SetBool() method to set the parameter we made to true or false depending on our movement.
Always remember to null check things like this. So the code basically says “if the distance between our guard and the currentTarget is less than 1 we walk “Walk”, the bool parameter we made to be set to false. Otherwise it will be set to true. The end result:
What in the world is that big green rectangle in front of the guard!? You ask. Well.. I’ll go over that in the next article.