Day 11: Using Unity’s animation system to create visual effects

Objective: To add visual effects to our game to make the gameplay even more attention grabbing.
The enemy has an explosion animation that we will trigger once an enemy is destroyed. This will be handled differently for the player and asteroid as they will use instantiation of game objects to create this effect. To animate the enemy we just need to select the enemy object and create an animation. This will auto add an animator component so that we can control the animation state through code. We have to create an animation parameter that we can control through code. This parameter will be used as a condition for the animator to transition into the next animation.

Now that we have our animator component set up for the enemy, we will now go into the enemy script and add hooks to the animator component. At the start of the game we will use GetComponent<desired component>() to assign the animator.

All we have to do now is use that parameter we created on the enemy’s animator to trigger the explosion effect. I used a bool for this feature but there is also a trigger parameter we could use.

Now that we know how to use the animator to play animations during gameplay, we can also make use of animation events to call public methods in the class attached to the object we want to animate. We want the enemy ship to disappear somewhere near the very end of its explosion animation. we don’t want to destroy it right away since this is an animation created specifically for the enemy ship. To do this you select the object to animate and in the animation window you scroll near the end of the animation and assign an animation event by clicking on the vertically taller icon circled in the image below.

The player and the asteroid will do this differently by instantiating an explosion effect object once the player runs out of lives. We also want to represent damage being done to the player. To do this we set up the two damage fires to each engine and parent these game objects into the player.

We will add hooks to the explosion effect and we will use an array to store the two engine fires. We will turn each engine fire on when the player receives two hits of damage. In the TakeDamage() method in the player script we can use a switch statement on the health value. The engine fires will appear one at a time with this system in place.
This set up is also applied to the asteroid.