Top-Down attack pattern for a simple enemy

I want to write about the attack pattern/method that I implemented into our shooter. This was implemented during the second sprint week of production. The method is used by the simplest enemy we will have in the game, the farmer, who simply walks towards the player and then does a thrusting attack that deals damage to the player. This specific attack pattern won’t be used by the more complex enemy types we will add, but the method I learned for implementing it will definetly come in handy, as it can be applied to different attack patterns too.

This was a completely new thing for me to code and was a bit confused on how I should approach the problem. First I tried to do it simply through code, without any animations, something like “if the player is in range, create pitchfork object, activate attack-hitbox, which then damages the player on collision” I quickly realized that creating the pitchfork and then moving it in a thrusting fashion was much harder than I had thought, plus it wouldn’t actually be viable to have nice animations this way, as the pitchfork would be a seperate object from the enemy. I decided to find an alternative method for this problem. After some researching I found out about the inbuilt animator and that I could use the animations itself to trigger events, such as activating hitboxes.

How I tackled the problem in the end was building a simple state machine using the inbuilt animator. The enemy has a walking state where it simply seeks the player. Once the player comes into range it changes into the attack state, which is actually split into three different animations states, each only a few frames long. The state machine can be seen in the picture below.

statemachine.PNG

The windup and recovery do nothing but play the animation frames. The AttackFatal state, where the enemy has extended its pitchfork fully, activates a collider which then damages the player on collision. AttackReturn has a transition to either Walk or AttackWindup, depening  on if the player is still within attack range.

As it is now the animation states are a bit wonky, they sometimes loop rather than transitioning to the next one, this is even visible in the GIF I have included in this post. However we have decided to keep them as is for now, as there is still a bunch of functionality to add to the game’s alpha version. I will probably look closer at fixing the problem before the Beta version.

The enemy attacking the player can be seen below.

attackAnim1.gif

About Leevi Hakala

2016 Programming