Objective: Create an asteroid that rotates and then explodes when hit.

First off, let’s create the gameobject by dragging in the sprite into the hierarchy. Give it the rigidbody2D and circlecollider2D components, then create a c# script specifically for it.

In this script we’ll be accessing the transform component to tell it to rotate.

Now we need to add another object to the hierarchy that represents the explosion. We’ll make this a separate object so that we can just instantiate it later and be able to give it its own script as well. Do this, and give it an animation until it looks something like this:

Now its time for creating the collision detection method, and have it react to when the collision’s tag matches “Laser”. Remember to make a gameobject variable to attach the explosion to, and instantiate it when the laser hits the asteroid.

Now go to the script for the asteroid. Here we’ll be telling it to destroy itself after a certain amount of time after starting so it won’t be clogging up the hierarchy. It’s just a simple line of code like so:

Now, we have an asteroid that explodes!

Currently learning to code with the help of GamedevHQ in order to someday my my game ideas come to life!