Day 18: Exploding!

In this article I will be talking about how to have some VFX happen when we destroy the enemies, when we are destroyed, and when we destroy this asteroid we’re about to implement.

Lets pull our asteroid sprite into the hierarchy. We’re going to make it to where no enemies spawn until this asteroid is destroyed. First add the necessary components for collision to happen. Circle Collider 2D and a rigidbody 2D. Set gravity scale to 0 and set is trigger to true on your collider.

We’re going to need a script to handle the asteroid behavior. Call it Asteroid.

As you probably guessed, we need some variables! We want the asteroid to slowly rotate. It looks cool and gives us a really spacey vibe. To do this create a float variable to handle to rotation speed. In the Update() Lets put the following code:

transform.Rotate(Vector3.forward * _rotateSpeed * Time.DeltaTime);

Whats going on here you ask? Well.. if you check out the Unity documentation for transform.Rotate you’ll see that you can “Use Transform.Rotate to rotate GameObjects in a variety of ways. The rotation is often provided as an Euler angle and not a Quaternion.” If this isnt exactly what we need I dont know what is! Instead of typing out all 3 axis’ or the Vector3 we can just say Vector3.forward. Forward is referring to the z axis. We want to rotate the sprite on its Z axis. Multiplied by the speed you set and Time.deltaTime. Easy! Now lets set up the laser collision to destroy it.

Disregard the audio related text. We’ll get into this later. Focus on the Instantiate and destroy lines. Here we’ve said that if the laser collides with the asteroid we’re going to instantiate an explosion prefab at the transform.position of our asteroid. The Instantiate method needs that info plus rotation info so we’ve just used Quaternion.identity for that. We’re gonna destroy the laser and then the asteroid after 0.25 seconds. Once that happens we call StartSpawning() from our spawn manager.

Remember, to get access to the spawn manager you need a variable for it.

We find the game object called “Spawn_Manager “ in our startmethod and then get component to get access to the script called “SpawnManager”.

I’ve removed the enemy spawn and powerup spawn info from our start method, deleted the start method and then made a method called StartSpawning() in out Spawn Manager Script.

So once the asteroid is destroyed our enemies start spawning :)

To create the explosion prefab that we instantiated earlier we need to just find the first frame of the animation, drag it into the inspector go to our animation window, create a new animation, hit the record button, and drag all the rest of the frames into that. Now you can prefab it by dragging into the prefabs folder.

In the inspector for the asteroid make sure you drag the appropriate game object into the Explode Anim Prefab Slot. I set my rotation speed to 19 in our inspector because I like that speed.

Hop into Unity and check out how it looks!

Well Hot damn! Lookit that! Now lets move on to the enemy. The animation for this a is a little different.

Here is the script we’re going to use. As you can see, we are in our OnTriggerEnter2D method.

First we need to create the _anim. To do that we need an animator component to reference on our enemy. Navigate to the folder containing the enemy explosion animation. Create it and then you’ll see the animator component on the enemy prefab. All we do to get access to that Animator component is the exact same thig we did for the SpawnManager in our Asteroid. We dont have to use GameObject.Find though because we are already on the game object that has the Animator component. Now…

SetTrigger(“OnEnemyDeath”); What the heck is that? Lets check out our animator controller that was also created when we made this animation.

On the left you’ll see this parameter. Navigate to the parameters tab, click the “+” sign. You’ll be asked what kind of parameter. create a trigger parameter and call it OnEnemyDeath.

Create an empty state by right clicking the area on the right. Set it to default by right clicking it. Now we need to make that transition to the Enemy Explode cell. Right click, go to create transition, and drag it from the empty cell to the Enemy Explode cell.

You’ll see on the right that we need a condition to make this transition happen. Click the “+” sign and you’ll see it automatically chooses OnEnemyDeath. Now when OnEnemyDeath is set the transition will take place causing the animation to take place. We set it in our OnTrigger method! Easy Peezy!

EXPLOSIONS!!!

We don’t want to destroy the enemy game object immediately. If we do, the animation wont play! So we just destroy the collider and the enemy script. We destroy the collider so that if the player collides with the explosion they don’t take damage, and the enemy script so they stop firing when they are destroyed. After 2.5 seconds we destroy the entire enemy game object!

The player exploding needs to take place once your lives < 1. I think you can handle that ;). Feel free to reach out to me with any questions on how to set that up. Happy animating and VFX-ing!

--

--

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