GameDev : VFX Effects (Part II)!

We was on choose of what parameter pass to the animation.
In this case, Trigger I think is the solution, so we click on Trigger, and NAME THE ANIMATION TRIGGER

Now that we have the right parameter to pass, we need to set inside the Animator Inspector the CONDITION to trigger it.

Now the animation is ready.
The logic here is :
“Set the animation parameter or parameters, so we can CALL IT / THEM WHERE WE NEED”.
And because the Animator is a component, in code we use it exactly like other component.
We need a its reference, and we call the animation exactly where we need it.

Let’s do it!!

In Enemy script we create the handle to the Animator and in Start we initialize it and make a Null check.

Then, if we search on Unity documentation the Animator

Let’s look at methods part, and find a SetTrigger()

The thing we have to do is simple call
animator.SetTrigger(“OnEnemyDeath”) where we need it.

Go to TriggerEnter2D, and check when Enemy collide with Player or with Laser

TIP : probably we want the animation start even when Enemy collide with the Shield.
For now I call only when it collide with Laser or Player.

If we make a try

nothing happen.
Let’s look at code

We check if collide with Player (or Laser), call the Damage() method, start the animation and destroy the object.
The logic is correct, so what’s happen.

The problem is here

In both cases, we try to run the animation, but in the NEXT FRAME we Destroy the object!

Basically, if we try to start the animation, but the object IN THE SCENE DON’T EXIST...

To fix this type of problem, we can call a delay into Destroy() method, so the object run the animation, THEN DESTROY ITSELF.

To make this “precise”, we can look at animation window and see how long the animation during.

It’s about 2.3f o 2.4f, so we inside Destroy we use a 2.6f, and try it

Let’s test it

The animation now works!

In the next article we finish to set up animation and fix some little bugs!

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!