Creating Enemy Explosions Part 2
In the Review I learned that _explode.gameObject.SetActive(true); should instead be _explode.SetTrigger(“OnEnemyDeath”); then in order make sure the enemy1 doesn’t keep moving through the space on desctruction I would set the _speed = 0; and to give the animation time to actually show on the destroy.ThisGameObject I added a (, 2.8f); this will give it a delay before the object is destroyed.
void OnTriggerEnter2D(Collider2D other)
{Debug.Log(“Hit: “ + other.transform.name);
if (other.tag == “Laser”)
{Destroy(other.gameObject);
if (_player != null)
{_player.Score();}
_explode.SetTrigger(“OnEnemyDeath”);
_moveSpeed = 0;
Destroy(this.gameObject, 2.8f);}
if (other.tag == “Player”)
{PlayerBehaviour player = other.transform.GetComponent<PlayerBehaviour>();
other.transform.GetComponent<PlayerBehaviour>().Damage();
_explode.SetTrigger(“OnEnemyDeath”);
_moveSpeed = 0;
Destroy(this.gameObject, 2.8f);}}
Lets Test:
FAIL: On Enemy Death Parameter doesn’t exist… so what did I miss
I removed the _explode variable and changed it to _anim and updated the code.
I learned that I never set the Parameter in the Animator. In Unity when I have the Enemy1 selected I can choose the Animator in the scene to get access to the parameters.
In there I created a new Empty state then Made a Transition to connect it to the Enemy1 Explosion. I then set the Empty state to the default.
Next I highlighted the transition line between the Empty and the Enemy Explosion and in the Inspector I added a condition for the OnEnemyDeath Parameter.
This should allow the code to work successfully. Let’s Test:
FAIL: I turned the Animator back on and tested it again
It works but the delay is very large.
In the controller I changed the settings and unchecked Has Exit Time then changed the Transition Suration to 0. Let’s Test
I shouldn’t have turned the Animator back on at the start. I turned it off and tested it again.
Ok that isn’t right either…
I turned the Animator on at the start then in the Animator I turned OFF the Parameter at the start. Let’s Test:
Now it works. It also has a secondary concern for the player where they must now also be wary of crashing into the enemy debris.