[Project Week 10] Player attack animation

Player attack animation

For this week I will be creating a player attack animation which will play out once pressing the space bar, later I will be binding different attacks to hotkeys 1, 2 and 3 or an action bar UI that will be on the screen to display the different attacks there are to choose from like frost, fire and lightning attacks.

Creating the animations

First of all I created all the animation frames then combined them all into one file so that it will play out depending on which position the character is at the time, so for attack up I selected all the frames and created a file called 'Attack_Up' and the same with all the different directions.

Here are all the different frames of the animations:

Here are the animation files with the frames combined:

Then I created a layer which I did previously for the idle and walk animations, this time it will be for attacking:


To do this I simply copied the previous layers I created then pasted them in the attack layer, I then renamed and changed the animations from 'walk' to attack, just to speed the process up a bit.

Player script

Then I wrote a function that will make the player attack, this function will simulate a cast time which will be something like 3 seconds, later I will be adding different spells which will have different cast times, but for testing purposes I will be using 3 seconds for all.

I created this function inside the player, just because the player and enemies will be attacking in different ways in the future, else I could have made this function inside the character script for general use.

This is the attack function:


The reason it's an iEnumerator is because if you are returning a iEnumerator you can return something called yield return new:


Where as if I used a void function this cast time wouldn't be possible.

This code starts the attack animation once a key is pressed:


Here is the key input that will activate the attack animation. The coroutine is basically a function that runs as well as when the rest of the script runs:


I had to add some things to the handle layers function within the character script so that attacking stops once moving. So if i'm attacking the attack layer animation is played, else if the player is moving then the attack animation stops:


Next I added a function that will stop the player from attacking, this will become more useful later when things like enemies are added which are able to interrupt the players attack:


I added some code that prevents my character from starting to cast the spell while moving, only when standing still:


There was a bug where my coroutine did not stop correctly so I had to create a function to stop it the proper way, first I created a reference to the coroutine:



Here is what the attack animation looks like in it's current state. As you see when I press space bar the animation plays, and once i move the animation cancels. So far everything is working as intended, next I plan on making the spell casting for the animation:


Comments