My 90 day journey becoming a Unity game developer: Day-27
Objective: Adding visual effects to the enemy being destroyed with explosions.
First, let’s open the enemy prefab. Go over to the animation tab if it’s open. If not go to Window->Animation->Animation to open the animation tab. Click on Create and name the animation Enemy_Destroyed_anim or something else you prefer.
Second, open the Enemy prefab where we will create an explosion animation that will be attached to the enemy. Go to the sprite folder where your explosion animations are located and select from the beginning of the animation to the end of it. Drag the animations over to the Animation window and press Play to see the explosion effects.
Now if we play the game, we will see all the enemy ships exploding instantly.
Third, we will need to turn Loop off in the animation clip to keep the explosions from exploding over and over. The animation clip will have a triangular shape in front of the name of the clip.
Fourth, the main reason the explosions will play repeatedly is that the Animation Controller is telling the explosion animation to play by default. Therefore, we need to go the Enemy controller. Notice the name of the controller isn’t the same as the animation. Let’s changed it to the same name without the anim at the end to easily identify the controller associated with the animation clip.
Fifth, open the Animation Controller by either double-clicking on the Enemy controller’s name which will have what looks like 2 nodes having lines connecting to each of them. Or if the Animator tab is open already, just click on the name of the animator controller.
Sixth, we will notice that the animation clip is in orange. This means that the clip is set in the default state. The controller is telling the animation clip to play right away.
But we don’t want to play the clip right away, so let’s create a new state and name it Empty. Also, set it as the Default state. If we play the game, the explosions will stop completely.
Last, we want to transition from our Default state to the Enemy explosion animation when the enemy is hit using parameters and conditions. Create a transition from Empty to Enemy Destroyed Anim state. Then click on the arrow in the middle of the transition. From there go to the Parameter tab and click on the plus (+) sign. Select the action Trigger, which is what we want to use and cause the animation to run. In the Inspector under Settings, go to Conditions and click on the plus sign. This will automatically add the OnEnemyDeath parameter to be used when the condition is true.
Next will be using code for the Enemy Explosion implementation.