Day 15

Frankie Rodriguez
3 min readDec 1, 2020
Cr: Mad Scientist.gif via Giphy

Enemy Explosion Setup

Today I began the process to bring my animation to life i.e. exploding enemy ships, having fire come from player thrusters, etc… Today I animated the enemies’ explosion. By default, the animation will play in a loop. Here is a video of how that looks.

Oh no. Oh no. Oh no, no, no.

Unfortunately, having the animation play on a loop is not something I want happening. Instead of a loop, I need to create a trigger to let the game and enemy know when to explode. To do this I opened the enemy animation script. I then clicked on Loop Time, thinking that would fix my problem. It worked, 50%. The loop was mended; however, my enemies were exploding when they spawned. Next, I went to the enemies’ controller aka the Animator View. There I was able to see the Enemy_Destroyed_anim was orange which told me that it was set in default. This meant that as long as the game object that is attached to the animation is active, the animation will continue to play. Because that is the case, I needed to find a way to tell Unity to slow her roll until it was time for the enemies to explode. First I create an empty state within Animator View. Creating a new empty state will become the default state. By doing so I was able to prevent the explosions from being set off.

Now what I have to do is tell Unity to slow her roll until its time, so I’m then able to do this is by creating an empty state within the animator view and having the new empty state become the default state. Once I did that, I was then able to prevent the explosions from being set off.

Next, I transitioned the empty state to the Enemy_Destroyed_anim. Unfortunately, I was only faced with the same outcome — enemies were exploding when spawned in. I realized it was due to the fact that no conditions were set in place to stop the enemies from exploding. To set the conditions I created Parameters, which lets the game object know what it is going to do and when. In the parameter, I created a trigger parameter titled EnemyDeath. From there I clicked on the transitioning line that connected the empty state and the Enemy_Destroyed_anim. Under inspector lies a condition list. There I added the EnemyDeath parameter. This will let the Enemy_Destroyed_anim know to not play unless the EnemyDeath parameter is triggered.

When button is activated, my enemies start exploding.

My next step is to find a way to control that animation directly through code.

Enemy Explosion Implementation

I started by opening the Enemy script. To have an idea of what I needed to do, I wrote a few pseudo codes to help me get on the right track.

Subsequently I start to tackle the //handle to the animator component. I first created a private Animator, which is the component. For my sake I named it _anim;. Then I moved to tackle the //null check player by using an if() statement. I coded it this way — if (_player == null) the Debug.LogErorr(“The Player is Null”). From there I wanted to assign the component to an anim. To do so, I used the code: _anim = GetCompenent<Animator>(); From there I null checked by using an if()statement. Null checking helps to let me know there is a problem.

Regrettably, this is a far as I got today. While I would love to immerse my self fully into the course work, life beckons me.

--

--