2D Mobile Game: Enemy Projectile Attack
In this article, we are going to cover how to create a projectile attack that will use an Animation Event to trigger the attack.
Attack & Projectile Animation Setup
We are going to modify the Spider Animation tree by removing the walk animation and only going between the Idle and Attack animation.
The animation is moving between the Idle and Attack states, but the Spider still has the movement code.
In the Spider script, we will just comment out the base movement function from the Enemy Class and this will stop the Spider from moving.
After importing the Projectile sprite sheet, we drag the first frame of the sprite sheet into the hierarchy and rename it Acid_Projectile. We will create a new Animation and Animator for the Projectile.
We will set the frame rate for the animation to 20 frames per second and reduce the size by 1/2.
This will need to be made a prefab, so we can instantiate it every time the Spider goes into the Attack animation. We create a Prefab folder and drag the Projectile game object into the Prefab folder. This will turn the game object to blue, which lets us know it is a Prefab. We can delete the projectile from the game scene now.
An animation event is a way to trigger a function on a certain frame inn an animation. In the animation window we select the frame we want to add the animation event to and select the Animation Event button.
When this animation event is selected, we can see in the inspector that we can choose a function we will want to trigger.
We will create a script (SpiderAnimationEvent) that will call this function and it will need to attached to the game object that has the animation event.
A public Fire Function will be created and this will be the one that is called from the animation event and for now, we will just debug log “Spider Should Fire” to make sure the animation event is accessing this function.
When we test it out, we do see the debug log message when the animation has reach the frame with the animation event.
Once we have the fire function being triggered by the animation event, we will need to have the animation event script be able to communicate with the spider script to instantiate the projectile. But first, we need a projectile script which will have the project move right at desired speed, self-destroy after 5 seconds if it doesn’t hit the player and if it does hit the player we deal damage to the player and destroy the projectile.
In the Start method, we will use the Destroy method which we can pass in a time that will tell when we want to destroy the the game object (5 seconds).
In the Update method, we use transform.Translate and pass in Vector3 right for the direction, a speed variable we can determine in the inspector and Time.deltaTime to make it meters per second.
Finally, we need to detect when the projectile has hit the player. Using OnTrigger 2D, we check the tag of the collider the projectile is hitting (other) to see if it is the Player. If it is the player, we get the IDamageable from the player and store it in the variable hit. Null check to ensure the IDamageable was stored in hit and then we can call Damage from hit along with destroying the projectile.
To detect collisions on the projectile, we will need to add a circle collider 2D and rigidbody 2D. With the circle collider, set trigger true and the rigidbody should have gravity to 0.
Animation Event Script
The animation event script can detect when a certain frame is played in the attack animation which will call a function from this animation event script. This function will handle the firing of the projectile from the enemy, so it will need to communicate with spider enemy script.
We create a Spider variable and get the component from the parent since the animation event script is on a sprite object parented to the Spider game object. Once we have a handle on Spider script, we call the Attack method from Spider in the Fire method which is being called from the animation event.
Spider (Enemy) Script
For the enemy script we will create the Attack method that is being called from animation event script, which will create or instantiate the projectile prefab.
The Attack method public so it can be called from the animation event script. We use the Instantiate method, which needs a game object to create or instantiate and that will be the projectile prefab. We need to create a variable that will store the projectile prefab and serialize it so we can assign the prefab in the inspector. We will need a position the projectile will be instantiate at which will be the position of the enemy minus the offset and we will want to keep the rotation the same (Quaternion.identity).
Lastly, in the enemy’s Damage method we subtract 1 from the enemy’s health and when it is equal to 0 we can destroy the enemy game object.