Previous: Reloading Now
Let’s make some visual effect to our enemy when destroyed by player. Open the Animation window from the top drop down menu Window>Animation>Animation.
Open the prefab of enemy, create an animation in Animation window.
When we create an animation, an animator will also created. Inside the Animation folder in Project, there will have be an animator called Enemy(which is a bug of Unity 2019, it should be the same name as animation). Double click and it should open the Animator window.
By default, it will automatically connect to animation just created. To prevent enemy got explosion when game starts, we can create an empty by right click in the Base Layer> Create State> Empty. And right click to it, set it as default.
And now we need add a condition to trigger the animation.
On the top left of Animator, there are 2 tabs. Click on Parameters where we can create parameter as a condition to start the animation. Click the add mark, choose trigger and name it OnEnemyDeath.
Then we can right click on the Empty layer> Make Transition, then drag it to connect with Enemy_Destroy_anim.
Click the transition arrow, we can adjust the setting of it.
Uncheck Has Exit Time, click the drop down Settings and set Transition Durations to 0, then add a condition and set the OnEnemyDeath to it.
Now we have set the animation condition, we can trigger it in enemy script.
In the script, set a variable of animator and get this component in Start().
There are 2 situation that enemy would be destroyed: collide with player and laser. These 2 are all need to trigger the animation. Add the SetTrigger function in these statement.
I added 2 more lines under the animator trigger.
One is turn off the collider to prevent trigger again with player, and one is to decrease the speed of enemy and the animation would be more realistic.
And also set a delay to destroy enemy gameobject, it would let the animation runs completely.
Now we can Play it and enjoy the animation.