Lab 1 - Introduction to Unity
Implement an endless bird game using given assets.
Your game should look similar to the following picture:
There are some questions between the following steps. You could try to answer them on your own or look help from internet. You don't have to submit the answers, they will just help you to understand Unity better!
And remember, when you are running your game in Unity editor, all the changes you are making in the scene won't be saved. So before you start making actual changes, make sure that your game is completely stopped and not paused!
Steps:
- Download the base file: http://optimatica.eu/courses/bird.zip
- Create a new Unity 2D project
- Create the following catalog structure under the Assets folder:
- Prefabs
- Scenes
- Scripts
- Sprites
- Sounds
- Animations
- Save your scene to the Scenes folder
- Copy all sprites, sounds, and scripts from the base file into your Unity project
- 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 to loop. For this, you have to open an "Animation" window and duplicate the frames from the beginning and move them to the end in reverse order.
- Add rigidbody2D and boxCollider2D to the Bird object.
- Make sure that the bird falls when you run the game now.
- 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. Because it is public it will be visible in Unity inspector window and you can assign a value there.
- Add a private rigidBody2D variable. In the Start() method find the RigidBody2D component from the object and assign it to the variable.
- In Update method, check if the space key is pressed. When pressed, set the velocity of the bird to zero and add an upward force to the rigidBody2D by changing the forceAmount variable of it.
- Adjust the gravity and forceAmount parameters of rigidBody2D until your bird moves naturally.
- Create a new empty object called "Tree".
- Drag the Tree sprite to the scene twice and name them "Top" and "Bottom".
- Parent these new objects under previously created "Tree" object and move them such that they are on the same vertical line and have a gap between them (watch the picture above). What happens if you try to assign the collider on the Tree object instead?
- Add a boxCollider2D component to both Top and Bottom parts.
- Make the Tree into a prefab and remove it from the scene.
- Add "Game.cs" script to the camera object.
- Attach Tree prefab to its corresponding variable.
- Adjust Game.cs parameters 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 a check to the Bird.cs Update method, that restarts the game if it falls out of the screen. You can use a method Game.Instance.Restart() that will reset the position of trees but also reset the position and velocity of the bird.
- Add a “void OnTriggerEnter2D(Collider2D other)” method to the Bird script. Make it restart the game and reset the birds' position and velocity when it collides with a tree. There is also another method OnTriggerEnter, when is this useful?
- Drag the background sprite to the scene.
- Scale it up until it covers the entire screen.
- Put the background sprite to a new sorting layer.
- 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 remove play on awake check box.
- 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.
Extra tasks (these are not necessary to get full points)
(they won't give any bonus points but are really good for practicing)
- 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.
- When the player dies, play the death audio too.
- 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 required to fill the screen with given tree distance. For a longer screen, it should make more than 3 trees.
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.
4. Lab 1 - Bird Game