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

Philip Johnson
3 min readJun 25, 2021
To give the player some breathing room before the game starts, they will have the ability to start the spawning of enemies and power ups after they destroy the asteroid at the top.

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.

we set the bool for isDead to true when an enemy collides with something. We also disable the colliders on the enemy so that we don’t accidentally run into it even though an explosion animation has been triggered.

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 use an array to store the engine fire objects for us to activate in the code based on how many lives the player has left.

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Philip Johnson
Philip Johnson

No responses yet