Creating Enemy Explosions
Objective: Implement enemy explosion effects
To start, go to the prefabs folder and double click the enemy prefab to open the prefab settings. Select the enemy in the hierarchy and go to the animation window. If it’s not open then go to Window, Animation, Animation. Create the animation and call it Enemy_Destroyed_anim. Select all the animation clips and drag them into the animation window and click play to view it.
Next in the animations folder select the animation and turn off loop. The animation is now playing instantly by default which we don’t want. Open the animation controller and create and empty state to use instead of the explosion animation. When you create the empty state, set it as the default state.
Now right click the empty state and choose make transition and attach it to the enemy destroyed anim state. Right now it will still auto play so we need to set up controls to make the transition wait till we call it. For this in the top left, select parameters. Select the add button and choose trigger and name it OnEnemyDeath.
Next click on the transition arrow between the empty and enemy destroyed states. In the inspector add a condition which for now will autoselect our OnEnemyDeath but if we had more you could choose which one to use.
Now open the enemy script and create a private Animator called anim. In the start method, check if the player exists with a null check if statement. Next assign the anim using get component and null check it. In the on trigger enter, for both the player and laser tagged if statements, activate the anim trigger. We have our enemy set to destroy but if we don’t add a delay the enemy will disappear before showing the animation. Set a delay in the destroy method (2.5 seconds for our animation).
To stop the enemy prefab from moving for that time, set the speed to 0 between the trigger and destroy call. The enemy animation has a slight delay as the empty state plays fully before transitioning to the explosion. To fix this open the animator, click on the transition between empty and enemy destroyed and in the inspector turn off has exit time so the transition is immediate when activated.
In the next post, we will add some damage effects to the player.