Day 110 — Setting up a Sword Arc Animation Effect
Hey and welcome!
There’s one last thing I want to talk about for animations for the time being and that is the sword arc effect we have that will play whenever the sword is swinging. After that I’ll show you how we’re going to be doing the camera follow in this which will be pretty quick to show how.
Let’s start with the sword arc, under Sprites > Effects you’ll find a Sword_Three_Particle sprite, go ahead and splice this one making sure that the dimensions are 512 x 512. Now drag and drop sprite 0 from the sprites that the slice creates into your Player object so that it’s the parent and then rename the object to Sword_Arc or similar.
Don’t worry too much about the position of this as we’ll fix that later but make sure that the order in layer for this matches your player sprite. With that done go ahead and click on your Sword_Arc object and create a new animation and record your sword arc sprites in this.
We’re going to something a little different for this one, you’ll notice that the animation goes pretty quick so drop the samples to 30. Then once you’ve done that select all of your frames apart from the first one and move them over to the right to the 0.05 seconds mark. If your animation window is currently in frames you can switch it to seconds using the 3 vertical dots menu in the top right.
With that slowed down, head over to your animator window for your Sword_Arc controller and create a new state called none and set it as the default state by right clicking it, this prevents the animation from playing as soon as the game starts. Now create a new trigger parameter called SwordAnimation and make a transition to your Sword_Arc state with this trigger as your condition.
Remove the exit time and set the duration to 0 for this transition so that it happens immediately. Now add a transition to your empty state and make sure that it has exit time and the duration at 0.25 otherwise it may not play properly. With that done it’s time to add some code to the PlayerAnimation script.
private Animator _swordAnim;void Start()
{
_anim = GetComponentInChildren<Animator>();
_swordAnim = transform.GetChild(1).GetComponent<Animator>();
}public void Attack()
{
_anim.SetTrigger("Attack");
_swordAnim.SetTrigger("SwordAnimation");
}
You should have your own Attack method in the script but if not you can add the SwordAnimation trigger to the bit of code where you’re doing your attack/sword swing trigger. One pretty cool thing that’s happening here is our code in the Start() method when we assign the Sword_Arc animator component to _swordAnim.
We can’t use GetComponentInChildren this time as there’s more than one child and this method focuses on whichever child object is first in the list. To get around that we can use transform.GetChild(1) to specify which child object we want, since the numbering starts at 0 we put in the 1 here for our Sword_Arc object, if we added in another object after this we would put in 2 and so on.
Before testing out your animation go into your Sword_Arc object and adjust the rotation values to 66, 48 and -80 in order to get a better looking effect for our sword swing.
You should have something like the above but it’s not lined up very well so I’ll go over how to fix that in the next one!