3 Programming Assignment 3 Detailed Instructions
3 Programming Assignment 3 Detailed Instructions
Detailed Instructions
Overview
In this programming assignment (the third increment of our Asteroids game development),
you're adding bullets to the game.
When you run your game, the asteroids should be moving at a more reasonable speed.
1. Add a sprite for your bullet to the sprites folder and drag it from the sprites folder onto
the Hierarchy window. Rename the game object in the Hierarchy window Bullet.
2. Add a Circle Collider 2D component to the bullet and edit the collider as necessary
3. Drag the Bullet game object from the Hierarchy window onto the prefabs folder in the
Project window.
4. Delete the Bullet game object from the Hierarchy window.
When you run your game, it should work just like it did at the end of the previous step.
1. Open up the Ship script in your IDE and declare a field called prefabBullet to hold the
bullet prefab you're going to instantiate when the ship shoots. Be sure to mark the field
with [SerializeField] so you can populate it in the Inspector.
2. Add an if statement at the end of the Update method that uses the Input GetKeyDown
method to see if the player pressed the KeyCode.LeftControl key in this frame. We're
not using an input axis for this input because we want to make the player release the key
and press it again to shoot another bullet. Add code to the if body to instantiate the
bulletPrefab with the ship's transform as the bullet's transform.
3. In the Unity editor, select the Ship game object in the Hierarchy window and populate the
Bullet Prefab field in the script with the Bullet prefab.
When you run your game, when you shoot the bullets they just sit there. That's because we
haven't made the bullets move yet; we'll do that soon. You may not realize it, but we're actually
detecting collisions between the ship and the bullets. Although that doesn't have any effect on the
ship or the bullets, it's actually inefficient. We'll fix that in the next step.
1. Select Edit > Project Settings > Physics 2D from the top menu bar. In the grid at the
bottom right in the Inspector, uncheck the box at the intersection of the Ship row and
Bullet column (it will show Ship/Bullet if you hold the mouse over the box). This
disables collisions between the ship and the bullets.
2. Select the Bullet prefab in the prefabs folder in the Project window and set its Layer to
Bullet.
3. This doesn't look quite right because the bullet appears on top of the ship. Select Edit >
Project Settings > Tags and Layers from the top menu bar. Expand the Sorting Layers
section in the Inspector if necessary. Add a layer named Bullet and another layer named
Ship (in that order).
4. Select the Ship in the Hierarchy window and set the Sorting Layer in the Sprite Renderer
component to Ship.
5. Select the Bullet prefab in the Project window and set the Sorting Layer in the Sprite
Renderer component to Bullet.
When you run your game, you should be able to shoot a bullet without destroying the ship. You
can drive the ship forward to see the bullet you instantiated. In fact, you could drive the ship
around leaving a trail of bullets behind you at this point ...
Step 5: Make it so asteroids are destroyed when they collide with a bullet
In this step, you're making it so asteroids are destroyed when they collide with a bullet. This
won't be the final game behavior, because larger asteroids will actually split into smaller
asteroids in the final project increment, but it's a step toward that final functionality.
1. Select the Bullet prefab in the prefabs folder in the Project window, add a new Bullet tag
to the list of available tags, and set the tag to Bullet. We need this tag because asteroids
are only destroyed when they collide with a bullet, not when they collide with the ship.
2. Add an OnCollisionEnter2D method to the Asteroid script. Add an if statement that
checks the tag of the game object that collided with the asteroid is "Bullet" (you should
use the coll parameter to access the required information). If the game object that
collided with the asteroid is a bullet, destroy both the bullet and the asteroid.
When you run your game, you should see that the asteroids are destroyed when they collide with
a bullet.
Step 6: Make bullets move
For this step, you're making it so the bullets actually move. The tricky part is making it so bullets
move in the direction the ship is pointing.
When you run your game, you should be able to shoot moving bullets that go in the direction the
ship is facing.
1. Copy the Timer.cs file you extracted from the zip file into the scripts folder for your
project.
2. Open the Bullet script with your IDE.
3. Add a constant field to hold how long the bullet will live (2 seconds) and a Timer field
that will hold the "death timer" for the bullet.
4. Add code to the Start method to add a Timer component, set the timer duration, and run
the timer.
5. Add code to the Update method to kill the bullet when the death timer is finished.
When you run your game, you should be able to see bullets disappear after about 2 seconds. You
can always temporarily change the constant in the Bullet script to shorten their lifespan to make
this easier to see.
Step 8: Make bullets screen wrap
For this step, you're making it so the bullets screen wrap. This is really easy with our
ScreenWrapper script!
When you run your game, you should be able to see bullets screen wrap.
1. Select Edit > Project Settings > Physics 2D from the top menu bar. In the grid at the
bottom right in the Inspector, uncheck the box at the intersection of the Bullet row and
Bullet column (it will show Bullet/Bullet if you hold the mouse over the box). This
disables collisions between the bullets.
When you run your game, you should be able to see that bullets don't collide with each other.
You'll probably have to do some driving and shooting to actually see this.
That's the end of this assignment. Just one more increment to go!