Objective: Implement our attack animation through code.
Picking up from our last tutorial we now need to create code to control the trigger parameter we created for our attack animation. If you have been following along with me through these tutorials than we already have a script to control the player, and a script to control the player’s animations.
Open the player’s animation script and we need to create a new method. This method will be in charge of triggering the player attack animation. This is done similarly to when we set the animation bool parameter “Jump” to true, or set the float value to the “Move” parameter.
Perfect! Now, we need to decide when we call this method. Shifting over to the player script we can see we have not created such a space yet. Let’s do this now. Inside the update method we can set up an input check. We also want to check if we are grounded, we only want to be able to attack while our player is on the ground.
When both of these checks are true we want to call the attack method on the player’s animation script.
Now when our player is standing still, on the ground, and left clicks, the player will perform the attack animation!
If you want to attack while moving we can! We need to navigate to the animator view and create transitions to and from the run animation and the attack animation.
On the transition to the attack animation we need to add the attack trigger condition. As well as set had end time to false, and the transition duration to 0.
On the transition back to run we need the condition of the move parameter to be greater than 0. Has exit time equals true and the transition duration is set to 0.
Now we can attack while moving and our player looks great doing so, this give us a sliding look to the attack!
As always, Don’t be afraid to experiment with your animations and I will see you in the next tutorial!