Day 25: Wrapping Up Our Combat System/ Animation Events

Philip Johnson
3 min readAug 10, 2021

Objective: To finish writing the code for our combat system by implementing a new type of enemy that fires projectiles and setting up our death animations.

Our combat system is set up with a pretty good foundation. Picking up where we left off last time I decided to take the code out of the Movement() method from both the giant rat enemy and the giant bird enemy. They share the same code that deals with them facing the player.

Enemy.cs will now handle the direction the enemy faces if they implement the parent class functionality.

Our bird and rat scripts will now look like this.

further modularizing our enemy code by adding common functionality to the parent class

To wrap up this combat system we will create an enemy that simply faqces the player and shoots a projectile. For this to happen we have to make use of Unity’s Animation Events.

We will create a new enemy out of the Giant Hamster model. This enemy will simply oscillate between the idle animation and the throw animation.

For us to call an animation event we need to have a script attached to the same object that has the animator controller. We can add an animation event into the animation in several ways. For this project we will use the animation window. We will create a FireTurret.cs script and attach it to the hamster.

this script needs to be attached to an object that animates with an animator controller

Now tat we have our animation event set up we simply call the methods we need to complete the effect. The FireTurret.cs script will search for the Turret class on start. Now whenever the hamster transitions into his throw animation, he will launch a projectile at the player. The bullet is a prefab of a fireball that will have a script attached to move its transform each frame to behave like a projectile. It will also live for about 5 seconds before it destroys itself. If the bullet collides with the player then it will search for the IDamageable interface and call the Player’s damage function.

The FireTurret.cs script calls a function inside of the Turret.cs script called Attack()

we instantiate a _bullet prefab at an offset in front of the turret and we have the bullet face the same direction the turret is dacing.

Now that we have our final enemy for this simple platforming game complete we can move onto the loot system. The turret effect can be seen in the gif at the top of the article. The death animations are strange because theyre just “failure” animations that came with the models. If this were a game I planned on publishing I would use quality art assets and animations.

--

--