Chapter 12. Asteroids and Damage
Asteroids
So far, you’ve got a ship flying around space, you’ve got indicators on screen, and you’ve got the ability to aim and shoot your laser cannon. What you don’t have is a legitimate target to shoot at. (The space station doesn’t count.)
So, it’s time to finally remedy this. We’ll create the asteroids, which will do not a huge amount by themselves besides fly around. Additionally, we’ll create a system that creates those asteroids, and flings them at the space station.
First, let’s make the prototypical asteroid. The asteroid will be composed of two objects: the high-level, abstract object that contains the collider and all logic, and an additional “graphics” object that’s responsible for providing the visible presence of the asteroid to the player.
-
Create the object. Make a new empty game object, and name it “Asteroid”.
-
Add the asteroid model to it. Locate the Asteroid model in the Models folder. Drag it onto the Asteroid object you just created, and rename the new child object “Graphics”. Reset the Position of the Graphics object’s Transform component, so that it’s positioned at (0,0,0).
-
Add a rigidbody and sphere collider to the Asteroid object. Don’t add it to the Graphics object.
Once they’re added, turn gravity off on the rigidbody, and make the radius of the sphere collider be 2.
-
Add the
Asteroid
script. Add a new C# script to the Asteroid game object, called Asteroid.cs, and add the following code to it:
public
class
Get Mobile Game Development with Unity now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.