Lab 1 - Introduction to Unity
Watch the lab Video
(Step 11 is only partially implemented in the video. You also have to make the game restart when the bird falls out of the screen)
In this lab, you are implementing an endless bird flying game.
Your finished game should look similar to this picture:
There are some questions between the following steps. You could try to answer them on your own or look for help from the internet. You don't have to submit the answers, they will just help you to understand Unity better!
And remember, while you are running your game in Unity editor, all the changes you are making in the scene will not be saved. So before you continue making changes, make sure that your game is stopped and not paused!
Steps:
- Create a new Unity 2D project
- Download the base file: http://optimatica.eu/courses/bird.zip
- Create the following catalogue structure under the Assets folder:
- Prefabs
- Scenes
- Scripts
- Sprites
- Sounds
- Animations
- Copy all sprites, sounds, and scripts from the base file into your Unity project
- Save your scene to the Scenes folder
- Separate BirdSpriteSheet into separate sprites
- Drag all bird's body sprites into the editor view at once.
- It asks you to save the animation file (save it under the animation folder).
- After that, it also creates a new 2D game object with the corresponding sprite and animation attached to it.
- Rename this object “Bird”.
- Make the birds' animation loop. To do this, you have to open the "Animation" window. By default the wing only moves down. Edit the animation so that the wing moves down and up again. To duplicate a frame you first need to move the timeline to the desired position (eg. at the end of the animation), then select the desired keyframe you want to duplicate and press Ctrl+C and Ctrl+V.
- Add rigidbody2D and boxCollider2D to the Bird object.
- Make sure that the bird falls down when you run the game.
- Create a new C# script to the scripts folder and name it "Bird.cs".
- Attach this script to your Bird object.
- Add a public float variable named ForceAmount to the Bird script. By making this variable public, it appears in the inspector window and you can change its default value from there.
- Add a private _rigidbody2D variable (type Rigidbody2D). In the Start() method find the Rigidbody2D component from the object and assign it to this variable.
- In the Update method, check if the space key was pressed. When pressed, set the bird's velocity to zero and add an upward force to the rigidbody2D (use the variable ForceAmount).
- Adjust and ForceAmount value and gravity parameter of rigidbody2D until the bird movement feels good.
- Create a new empty object called "Tree".
- Drag the Tree sprite to the scene twice and name them "Top" and "Bottom".
- Parent the Top and Bottom under Tree object by dragging them in hierarchy. Then adjust their position such that they are on the same vertical line and have a gap between them (watch the picture above).
- Add a boxCollider2D component to both Top and Bottom parts. What happens if you try to assign the collider to the Tree object instead?
- Make the Tree into a prefab by dragging it to the asset folder and remove it from the scene.
- Add "Game.cs" script to the Main Camera object.
- Attach Tree prefab to its corresponding variable.
- Adjust the parameters of Game.cs script until it spawns trees with reasonable spacing.
- Make the game restart when the bird falls out of the screen -
- Change birds' boxCollider2D to a trigger. What does it change?
- Add Unity event that triggers when the bird falls out of the screen, or check it in the Update method. When it does reset the position and velocity of the bird and also read the implementation of Game.cs script to find a way to reset the position of the trees as well.
- Add a “void OnTriggerEnter2D(Collider2D other)” method and make it restart the game as well. There is also another method OnTriggerEnter, how is it different?
- Drag the background sprite to the scene.
- Scale it up until it covers the entire screen.
- Create a new sorting layer (there is a "Layers" button on the top right corner). Click on it, choose edit layers and make sure you add a sorting layer, not a regular layer.
- Put the background sprite to the new sorting layer. (Pay attention that there are additional parameters in the sprite renderer component)
- Order this sorting layer to be drawn first. What is the difference between regular layers and sorting layers?
- Add an AudioSource component to the bird object.
- Add the jump sound to the audio source and uncheck the play on awake checkbox.
- Create a private variable _audioSource and assign the AudioSource component to it.
- Every time the bird jumps play the AudioSource.
- Build your game and test it.
- Your bird might have been disappeared in build. To fix this, untick the "Simulated" checkbox in Bird's Rigidbody2D component and add the following statement to your if statement that checks when the Space key is pressed: _rigidbody2D.simulated = true;
This bug happens because the Bird fells out of the screen before Unity has time to start everything. The latest Unity version then hides the bird when it happens.
Pick and implement 3 out of 7 of the following tasks:
- When the player dies, play the death audio too.
- Make the bird tilt up or down depending on the movement direction (down if the bird is falling).
- When the bird hits the wall make it fall out of the screen first, instead of resetting the game right away.
- Add a score counter to the screen. Increase the score every time when the player passes through the trees.
- Change the Game.cs script such that it creates as many trees as needed to fill the screen with a given tree distance. Decrease the tree distance so that the game shows at least 5 trees at once. (make sure that the game remains playable)
- Make the tree parts move up and down periodically. (make sure that the game remains playable)
- Make each tree spawn with a random colour.
Before submission, playtest your game, tweak the inspector values (ForceAmount, Tree Distance, Tree Speed, etc.) until the game feels enjoyable.
Submission
Submit a compiled version of your game (Windows 64 build). Zip your build folder into a single package and make sure that all the additional files (that were created when building the game) are also included in this package.
Because it is your first submission, unpack it in another folder and make sure that you can still run it in there - just in case!
Task deadline is before the next lab.