Below you can see our warrior. When he swings his sword, we see a cool arcing effect that makes the scene more visibly pleasing. Here I will go through the steps to add this.
The first thing to do is to add a sprite to the player game object, here I will call it Sword_Arc.
Once we have the sprite added, we need to create an animation that displays the arc, which is made up of several sprite images. Adding the animation will also automatically add an animator controller to your game object.
Once we’ve created the new animation clip, we need to add the frames to the clip in the animation window:
Next, adjust the sorting layer of the sword_arc’s sprite renderer so that it is on the same layer as the player sprite.
Now that the animation is visible, we can see that it is going too fast and is not in the right position. I started by lowering the sample right of the animation and then spreading out the frames a little bit:
Once we get the animation frames and speed correct, all we need to do is position and rotate the animation so that it looks correct for when our player swings his sword. This will take a little trial and error to find what looks best.
Once we have the animation and sprites set up properly, we can now adjust the animator and code to play the animation properly. Here I have a controller with a nothing state and a sword arc state.
I use a trigger to signal the transition to the sword arc state. The transition has no exit time or transition duration as we want the transition to happen immediately upon a sword swing.
I then allow the sword arc to play and then transition back to the nothing state when it is done playing. This is done by setting the animation to has exit timed, with a 0.25 exit duration to blend between the states.
After the animator is set up we only need to trigger the animation when the player swings the sword. This is done in the player’s attack code. It is done at the same spot in the code where we trigger the sword attack animation.
Here is the code that triggers the animations. Pretty short and sweet:
And that’s pretty much all there is to it.