Day 16

Today I continued to work on implementing the enemy explosion animation. Continuing from where I left off, I created a trigger animation within the code. When the enemy collides with the player or the laser, the explosion animation is triggered — _anim.SetTrigger(“EnemyDeath’); — I placed this code in if()statements of both the player and the laser. Reference lines 64 & 77.

However, I soon realized that implementing that code was not enough. When I ran the game, the enemies continued to explode. I was stuck for a while wondering why the explosion kept occurring. Then I realized, the EnemyDeath trigger was set to active.

As you can see above, when the trigger is set to active (white), the enemies spawn exploding. When the trigger is off, the enemies come down normal.

Once I set the trigger default to false, I ran into another problem — the enemy would not explode once it collided with the player or laser. Why??? The answer lies within the Enemy Script. Looking over my codes, I realized the gameObject, the enemies, were destroying themselves but did not allow the EnemyDeath animation to play. To fix this, I needed to give the EnemyDeath animation time to play before the enemy (gameObject) destroyed itself. Within the if() statements I created for both the player and laser, I needed to change the Destroy(this.gameObject); code to add the amount of seconds I want the EnemyDeath animation to play. For my sake, I used 2.3 seconds before the enemies are destroyed. The code changed from Destroy(this.gameObject); to Destory(this.gameObject, 2.3f);. Once I resolved the timing issue, I faced another problem, my enemies continued to move while exploding. Why was this a problem? My player now had the problem of having to jface exploding enemy ships that had the capability to take a life….a mid-level boss on steroids.

To prevent this massacre, I needed to have the enemy stop moving once it got hit by the laser or player. Of course, if the player ran into the stationary exploded ship, the player will lose a life. To prevent the enemy from moving once it’s hit, I went into the Enemy Script. Within the if()statement of the laser and player, I place _speed = 0; — Reference line 65 & 79.

Once these changes were made and saved, I hopped back onto Unity to check out the results and this is how it turned out.

Above the game screen is a picture of the delay time meter.

After running the game for a while, I began to notice a delay in the EnemyDeath animation and was left to figure out why it was occurring. I went to the Enemy animator while playing the game and noticed a delay time was present. The transition from the empty state to the Enemy_Destroy_anim could not be completed until the delay time was filled. I looked under the Inspector -> transition line ->Has Exit Time setting to make the necessary changes. It is there where I set the exit time to false and changed the transition duration to 0. Once I saved my changes and ran the game, the animation reaction was no longer delayed when collision with the player or laser occurred.

I apologize for the blurry video. But as you can see, the explosion has been implemented and running smoothly. I can’t wait to further develop the animation.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store