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.
Our bird and rat scripts will now look like this.
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.
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()
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.