Objective: Add VFX to your enemies when destroyed
At this point you will have multiple Scenes, and it’s important to work on the correct scene, so check often.
Open the enemy prefab view and navigate to the animation window. You will need to precure your own set of explosion sprites to use.
Create a new animation file and add all your sprites by selecting them all and dragging them into the window. Once you are happy with the animation you can close the prefab view.
We only want to call this animation once. Select the animation and in the inspector uncheck the loop function.
Open the controller with a double click and right click on the animator view to create an empty state and rename Empty.
Right click the empty state and set as layer default state. This gives us control on when we call the Enemy_Destroyed_anim. Right click on the Enemy_Destroyed_anim and make transition.
We control this transition with parameters. There are four types of parameters. (Float, int, bool, Trigger) For this tutorial we want to use trigger. Label this as OnEnemyDeath.
Select the transition line and on the inspector view there is a component for condition. Select the only condition available, OnEnemyDeath. That is it for the prep, now we are ready to code.
Open the enemy script. We need to start with creating a handle for the animator component.
Now assign the component to the handle you’ve created. Since the animator is already connected, we don’t need to use the GameObject.Find.
We want to set the trigger where you have your Enemies being destroyed. I have two locations, one for the player collision, and the other for the laser collision.
now the code isn’t told to wait, so if you played the game now you would not see the animation you created. This is due to the next line after we trigger the animation to start, we have the object destroy itself. You can fix this by adding a float value equal to the time of your animation.
The next issue you may notice is when your animation is playing your player can still be hit by the enemy. A simple fix for this would be to stop the enemy in its tracks by setting its speed to zero. Now you could still get damaged by running into it while it’s exploding, but that would also happen in real life, so I’ll allow it.
That is all there is to it. You have now successfully added animation to your enemies’ deaths. You can now create animation for other events you have prepared for your games. Don’t be afraid to experiment with your code, and I’ll see you in the next tutorial.