Explosion Animations For My Game
Today, I worked on animation and added visual effects to the enemies when they are shot. I also adjusted the size of the player and the enemy’s sprite to make it less claustrophobic on screen.
I begun my work by switching out the enemy’s sprite with its own unique sprite.
I create a new animation called, 'Enemy Destroy’ and drag the sprite sheet into the time line. (Yes they appear upside down in game. It was a happy accident).
I adjust the Enemy_Destroy state speed to 4 so that my animation is more crisp and my code execution quicker. Prettier animations and faster clean up of wasted enemies.
The animation speed could also be adjusted in the Animation window.
I then create a trigger called,’ED’ to be able to trigger the explosion animation in our code. I make sure I have the default state on idle so our animation will not transition into the explosion state on start.
I prevent that by creating a transition from entry and Idle that’ll only allow transition to our explosion state(Enemy_Destroy) when ED is triggered.
I set the exit time to zero so that the transition can occur anytime the idle state is being played. I can uncheck the ,’Has Exit Time’ and have the same effect, but I usually keep it checked because of strange errors I had in the past.
I make a reference to both the enemy’s animator and collider2D to be able to access our newly created Trigger parameter and our enemy’s collider.
Since our script is attached to the game object, its simple. We declare the references of our components and assign them in Start(). I left out the null check since this is a prefabbed game object. Meaning that the components should always be attached unless I purposefully remove it.
I don't want my game to execute code if it doesn't need to.
The enemy will have its collider disabled using our newly declared collider component reference(EnemyCollider) and using the EnemyCollider.enabled command.
We disable the enemy collider because the animation must be played before the enemy game object is destroyed. This means that the game object as well as the colliders will exist in game space for the amount of seconds it takes for the animation to finish. This may damage the player even when it has been shot.
Plus the enemy will only absorb the amount of shots needed to be destroyed; Letting the lasers flow through the explosions.
The enemy’s speed is reduced to 1 for visual effect.
Lastly we call the trigger and add a value to the second parameter of Destory(). This will delay our Destroy() function for 0.6f seconds. Which is plenty enough time to see the explosions.