Creating Objects
Tutorial
·
Beginner
·
+10XP
·
30 mins
·
(195)
Unity Technologies

You will create a new GameObject that allows the player to destroy obstacles
Tutorial Outcome
By the end of this tutorial, you will be able to
- Add a (trigger) Capsule Collider 2D to keep track of when objects touch or intersect
- Add a Bullet attribute script to keep track of points
- Create a prefab
- Create an empty GameObject
- Add the Object Shooter script
- Insert a Prefab as a Prefab to Spawn for the Object Shooter script
Key concepts:
- Capsule Collider 2D
- Trigger Colliders
- Bullet Attribute script
- Prefab
- Object Shooter script
Challenge Ideas:
- What would happen if the laser was not turned into a Prefab? Would the lasers still work in the same way? Why or not?
- What would happen if the player could gain points for destroying asteroids? How would it change the way the game ends?
Languages available:
1. Create a Laser Prefab

The Capsule Collider 2D should cover the projectile as close to the cylinder shape as possible

Does your Capsule Collider 2D look something like this? The size might be slightly different, but make sure that the other values are the same
You’ll introduce health so that objects can damage each other on collision
- Go to Project > Assets > Images > Projectiles and drag LasersMid into the Hierarchy
- In the Inspector of LasersMid, click on Add Component and add Capsule Collider 2D and change the Direction to Vertical. Tweak the Size until it just covers the projectile. Now, tick off the check mark next to Is Trigger
- Also add Rigidbody 2D to allow the projectile to move and make the Gravity 0 as you did for the other Rigidbody 2D components
- Now add the Bullet Attribute script to keep track of who shoots the laser so that the game knows to whom to assign points to
- Now that all of the necessary components have been added to the laser, you will make it a prefab by dragging LasersMid from the Hierarchy into Project > Assets > Prefabs. Once you’ve checked that LasersMid has been transformed into a prefab, delete LasersMid from the Hierarchy

When you drag LasersMid into the Prefabs folder, you will notice that it looks like a blue cube, which means that it’s been turned into a prefab
Definitions
- Bullet Attribute script: holds a reference to which player initially shot it
- Prefab: a “master copy” to generate GameObjects from
Did you know?
- Trigger colliders are used when you need to detect when two objects touch or intersect each other without the components applying a push or resistance
2. Create a Laser Shooter

Make sure that SpaceshipRed is parenting LaserShooter (the empty GameObject that you renamed)

Make sure the green arrow and circle appears in the front tip of the ship
You’ll introduce health so that objects can damage each other on collision
- Click on SpaceshipRed in the Hierarchy and then right click and click on Create Empty to create an empty GameObject. Right click on it and rename it LaserShooter
- While clicking LaserShooter in the Hierarchy, go to the Inspector > Add Component > add Object Shooter script. Next, drag and drop the LasersMid Prefab that you created in Step 1.1 and into Prefab to Spawn. Feel free to change Key to Press (the key that will be pressed to trigger the laser) to something else
- After the script has been added, you will notice a green arrow and circle appears in the game window. Move this to the front tip of the ship
- Now, click on the asteroid in the Hierarchy. In its Inspector window add the Destroy for Points script so that the asteroid will be destroyed on collision with the laser. Change the value of Points Worth from 1 to 0 because the purpose of this game is not to gain points for destroying the asteroids
- Press play to test the game and notice that you can destroy the asteroids

When you click on the key that you set for LasterShooter’s Object Shooter script, you can shoot lasers and destroy asteroids
Definitions
- Object Shooter script: creates copies of a Prefab and shoots them out
- Destroy for Points script: removes an object on collision and awards the Player points