My 90 day journey becoming a Unity game developer: Day-29

Objective: Add an asteroid that the player must destroy. Give the asteroid behavior that will make it rotate around. Plus, player will be able to destroy the asteroid with its laser.

Final result of the asteroid behavior.

To begin, drag the asteroid sprite into the Inspector. Then drag the asteroid into our Prefab folder to make it a prefab. Open the prefab, go to Sprite Renderer and change the Sorting Layer to Foreground. Also, add a Circle Collider2D and adjust the collider around the asteroid. Turn Is Trigger on so the player doesn’t bounce off the asteroid, instead it will go into the asteroid. Last, add a Rigidbody2d and set the Gravity Scale to zero so the asteroid won’t fall down the screen immediately.

Set the asteroid to the Foreground layer, added a circle collider, and a rigidbody with no gravity.

Next, create an Asteroid script and drag it onto the asteroid itself. Go to the Asteroid script and let’s make the asteroid rotate. There was 2 ways I came across making the asteroid rotate with the first using a rotation angle of 90 degrees and a rotation speed of .01 per seconds to rotate. You can play around with the rotation speed numbers to see how fast you want the asteroid to rotate. In the Update() function, we used the transform.Rotate() function passing in the 90 degree angle to the Z-axis, and multiply the angle by the rotation speed. Also, the other parameter we used the Space.Self to rotate the asteroid around its locale rotation. It might be better to use Space.World instead and rotate around the X and Z-axis in the World coordinates.

Rotating the asteroid using transform.Rotate() and the parameter Space.Self.

The other way to make the asteroid rotate is to use transform.Rotate() and use the Vector3.forward function which is basically the 3 axis defined as (0, 0, 1) to make the asteroid move along the Z-axis. Multiply the Z-axis movement by the rotation speed and by real time as well. Play around with the rotation speed to make the asteroid rotate around to your liking. We will stick with this solution going forward because it has less code to work with.

Rotating the asteroid using transform.Rotate() and the parameter Vector3.forward.

We will create an explosion animation. In the Hierarchy, create an empty game object name Explosion. With the Explosion clicked on, create a new animation and name it Explosion_anim or something to your liking. Drag all the explosion sprites onto the animation window and press play to see if it plays correctly. Go to the Animations folder and select the Explosion_anim. Turn off the Loop Time to keep the animation from playing over and over. When finished drag the Explosion game object into the Prefabs folder to make it a prefab.

Explosion animation created.

In the Asteroid script, we want to check for a laser collision. When the laser collides with the asteroid, we want to instantiate the explosion at the position of the asteroid. After that we want to destroy the laser, asteroid, and explosion game objects. To destroy the explosion, we will create a new script name Explosion and attach it to the Explosion prefab. Inside the Start() function, destroy the explosion game object in 3 seconds.

Laser collides with the asteroid and instantiates the explosion, plus the laser and asteroid are destroyed.
Destroys the explosion after 3 seconds as soon it’s called upon.

Click on the Asteroid prefab, drag the Explosion prefab into the Explosion Prefab slot located in the Asteroid script component.

Asteroid destroyed.

Next time we will control the spawn wave through the asteroid being destroyed.

Learning to become a Unity game developer.