Day 117: Attack animation

Objective: Set up a method to attack and add animation to it.

To start, We will add the animations in for the attack.

In the animator, we will need to set up transitions from the idle and run to the jump.

For the condition, we will create a trigger named Attack.

To leave the Idle and Run animation, we will have Has Exit Time and Fixed Duration off and duration set to zero. The condition will be Attack.

To go back to Idle or run we will change the condition, Idle will have no condition.

Next will be back to run, We’ll check the move speed, if it’s greater than zero keep running.

Now to handle the logic. In our animation script we will create a method for the attack. This method will set the trigger.

In our player script we will create a method to handle the attack.

In here will will want to check for input of the left mouse button and to know if we are grounded.

Inside this we will just call the method from the animation script.

This is just for the player attack.

To get the effect we will create a new sprite object under the player object. We will name it Sword Arc.

With Sword arc selected we will create a new animation.

In the animation window, we have the regular sprite animation, but I added a alpha animation. I set it at zero that goes to 255 on the alpha by .02 seconds of the animation. This is what is needed for my animation, yours could be different.

Our sprite is at the wrong direction, after playing around I found that my sprite needs to be at the following positions.

In the Sword arc animator, the animation is set as default, we will create a empty state and set as default.

We will need a new parameter trigger.

For transition from new state to the SwordArc animation we will set the condition as SwordAnimation.

Back in our animation script we will need to get a handle of the sword arc animator.

In start, we can’t just use GetComponentInChildren<Animator> for this one. This grabs the first child that has it. We need to get the animator from the second child sport. We will do this by using transform.GetChild(1).GetComponent. GetChild acts as an array and since arrays start at zero, our second child will be one.

In our attack method we will just add another set trigger.

All that is left is to flip this sprite when the player flips.

In our player script, we will grab the sprite renderer of the second child the same way we did for the animator.

In our Flip method we will flip the sword arc on the X and Y. We will also get the sprite a new position to get the position in the right spot. After playing with the positions we need to adjust the x position depending on facing right or left.

That is all for the Attack. Thank you for reading and have a wonderful day!!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tyler Smallwood

Tyler Smallwood

46 Followers

I am passonate on learning to program and use Unity to become a skillful Unity Developer