Chapter 3 Programming Assignment Detailed Instructions
User Manual:
Open the PDF directly: View PDF .
Page Count: 4
Download | |
Open PDF In Browser | View PDF |
Programming Assignment 3 Detailed Instructions Overview In this programming assignment (the third increment of our Asteroids game development), you're adding bullets to the game. Step 1: Slow down the asteroids For this step, you're slowing down the asteroids (if they're going too fast as they are in my solution to the previous programming assignment). You can slow the asteroids down by reducing the range for the impulse force that's applied to get the asteroid moving. When you run your game, the asteroids should be moving at a more reasonable speed. Step 2: Make bullet prefab For this step, you're making a bullet prefab. We need a bullet prefab so the ship can fire bullets at run time. 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. Step 3: Make ship fire bullets For this step, you're making it so the ship can fire bullets. 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. Step 4: Set ship and bullets to not collide For this step, you're making it so the ship and bullets don't collide with each other. Although we could punish the player for shooting themselves, we won't do that in this case. 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. 1. Add a Rigidbody 2D component to the Bullet prefab. 2. Add a Bullet script to the scripts folder in the Project window and open the script with your IDE. 3. Add a documentation comment above the class. 4. Declare a new public ApplyForce method with a return type of void and a Vector2 parameter for the direction of the force to apply. 5. Add a documentation comment above the method 6. In the body of the method, declare a constant for the magnitude of the force to apply to the bullet. You'll probably need to experiment with this to get a reasonable number. 7. In the body of the method apply an impulse force of the force magnitude * the force direction (the parameter) to the Rigibody2D component. 8. Go to the code in the Ship script where you actually fire a bullet. After instantiating the bullet, add code to get the Bullet component attached to the bullet game object and call the ApplyForce method for that Bullet object. You should be happy to know that the thrustDirection field in the Ship script is exactly what we need to pass in as the argument to this method. 9. Attach the Bullet script as a component of the Bullet prefab. When you run your game, you should be able to shoot moving bullets that go in the direction the ship is facing. Step 7: Destroy bullets after a certain period of time For this step, you're making it so each bullet is removed from the game when it's been "alive" for a certain period of time. If we left bullets alive forever, the player could just fill the screen with bullets and just dodge the asteroids until they're all destroyed (especially after we implement screen wrapping for the bullets). 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! 1. Drag the ScreenWrapper script onto the Bullet prefab. When you run your game, you should be able to see bullets screen wrap. Step 9: Set bullets to not collide with each other For this step, you're making it so the bullets don't collide with each other; it would look weird for bullets to bounce off each other in the game. 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!
Source Exif Data:
File Type : PDF File Type Extension : pdf MIME Type : application/pdf PDF Version : 1.5 Linearized : Yes Author : Dr. T Company : UCCS Create Date : 2017:10:30 05:35:41-06:00 Modify Date : 2017:10:30 05:35:46-06:00 Source Modified : D:20171030113207 Tagged PDF : Yes XMP Toolkit : Adobe XMP Core 4.2.1-c041 52.342996, 2008/05/07-20:48:00 Metadata Date : 2017:10:30 05:35:46-06:00 Creator Tool : Acrobat PDFMaker 9.0 for Word Document ID : uuid:0e53fe7b-9643-4772-98b2-50531d121b1d Instance ID : uuid:c86e48d5-2e0b-4f41-885a-894626afb5c9 Subject : 691 Format : application/pdf Title : Chapter 3 Creator : Dr. T Producer : Adobe PDF Library 9.0 Page Layout : OneColumn Page Count : 4EXIF Metadata provided by EXIF.tools