Rocket Boost
Game Design
Game Design
The player will need to fly a rocket.
The player will need to fly a rocket.
From one side of the screen to the other.
From one side of the screen to the other.
Move using boosters.Â
Move using boosters.Â
Avoid obstacles along the way.
Avoid obstacles along the way.
Level Design
Level Design
Let's create the: GROUND
Let's create the: GROUND
Add a Cube Game Object.
Add a Cube Game Object.
Make sure it is at the center of the world (0,0,0)
Make sure it is at the center of the world (0,0,0)
Change it's position and scale as shown in the screenshot.
Change it's position and scale as shown in the screenshot.
Rename your Game Object from Cube to Ground.
Rename your Game Object from Cube to Ground.
Make a material for the ground, choose a brown shade and apply it to your Game Object.
Make a material for the ground, choose a brown shade and apply it to your Game Object.
Add materials for each new Game Object to distinguish them.
Add materials for each new Game Object to distinguish them.
Make each Game Object you have created a Prefab.
Make each Game Object you have created a Prefab.
Prefabbing objects has many advantages:
Prefabbing objects has many advantages:
They are immediately ready for use in other levels.
Allows you to make a change to a Game Object and roll it out accross the entire game.
Allows you to have multiple versions or copies of a Game Object in use.
Your scene should look similar to this at the end.
Create a new C# Script and call it Movement
Create a new C# Script and call it Movement
Attach the Script to the Rocket.
Attach the Script to the Rocket.
At this point we should organise our project into folders for Prefabs, Materials and Scripts.
At this point we should organise our project into folders for Prefabs, Materials and Scripts.
Controlling the Rocket
Controlling the Rocket
We will apply forces to the RigidBody of the Rocket:
We will apply forces to the RigidBody of the Rocket:
Let's test the thruster:
Let's test the thruster:
Rotating the Rocket
Rotating the Rocket
Code refactor point
Code refactor point
Computer programmers and software developers refactor code toÂ
Computer programmers and software developers refactor code toÂ
improve the design, structure and implementation of software.Â
improve the design, structure and implementation of software.Â
Refactoring improves code readability and reduces complexities.Â
Refactoring improves code readability and reduces complexities.Â
Working with Prefabs
Working with Prefabs
All of the changes we made to Rocket have not applied to our Prefab.
All of the changes we made to Rocket have not applied to our Prefab.
This is because we made changes to the INSTANCE not the Prefab.
This is because we made changes to the INSTANCE not the Prefab.
We need to override the Prefab with all of our changes.
We need to override the Prefab with all of our changes.
Add an obstacle to the level.
Add an obstacle to the level.
Make sure it has a material.
Make sure it has a material.
Make it a Prefab.
Make it a Prefab.
Freeze the Rocket's Z position.
Freeze the Rocket's Z position.
Now Rocket can only travel on the X and Y planes.
Now Rocket can only travel on the X and Y planes.
Freeze the X and Y Rotation too.
Freeze the X and Y Rotation too.
Remember to work in the Prefab not the instance.
Remember to work in the Prefab not the instance.
There is a bug in the game where the when we collide with an obstacle the physics system (Rigidbody) conflicts with our manual rotation.
There is a bug in the game where the when we collide with an obstacle the physics system (Rigidbody) conflicts with our manual rotation.
Let's fix this:
Let's fix this:
If you want to make the movement more realistic, add some Drag to the Rigidbody.
If you want to make the movement more realistic, add some Drag to the Rigidbody.
AUDIO
AUDIO
Audio in Unity requires 3 things:
Audio in Unity requires 3 things:
An AudioListener Component
An AudioSource Component
An Audio File
The Main Camera already has an AudioListener on it.
The Main Camera already has an AudioListener on it.
Add an AudioSource to the Rocket.
Add an AudioSource to the Rocket.
Below is the Audio File for the Rocket.
Below is the Audio File for the Rocket.
Drag the Audio File to the AudioClip in the AudiSource.
Drag the Audio File to the AudioClip in the AudiSource.
Testing using Tags and Switch / Case Statements
Testing using Tags and Switch / Case Statements
We will test our collisions by tagging objects.
We will test our collisions by tagging objects.
Go ahead and tag objects in your level as the things which they represent (Eg: Ground, LandingPad, Obstacle, Pickup etc.)
Go ahead and tag objects in your level as the things which they represent (Eg: Ground, LandingPad, Obstacle, Pickup etc.)
Create a new C# Script to handle our collisions.
Create a new C# Script to handle our collisions.
Call it CollisionHandler.
Call it CollisionHandler.
Attach it to the Rocket.
Attach it to the Rocket.
Create the OnCollisionEnter Method.
Create the OnCollisionEnter Method.
Switch is often more readable than multiple IF statements.
Switch is often more readable than multiple IF statements.
Note that depending on your Game Objects and Tags you will have different messages appearing here.
Respawning using SceneManager
Respawning using SceneManager
This game has instant death.
This game has instant death.
If our Rocket hits any obstacle it explodes.
If our Rocket hits any obstacle it explodes.
We need to Respawn the rocket to try again.
We need to Respawn the rocket to try again.
We do this by reloading the scene using SceneManager.
We do this by reloading the scene using SceneManager.
Go to File --> Build Settings
Go to File --> Build Settings
We need to add our Scene to the build.
We need to add our Scene to the build.
As it is currently empty.
As it is currently empty.
Once you have added it, note that it can also be referenced by its index number.
Once you have added it, note that it can also be referenced by its index number.
We first add SceneManagement to the NameSpace.
We first add SceneManagement to the NameSpace.
Create a method to Reload your Scene.
Create a method to Reload your Scene.
Call it as the Default Case.
Call it as the Default Case.
It's bad practice to hardcode.
It's bad practice to hardcode.
Instead of using the Scene Index Number, we should determine what the current scene is.
Instead of using the Scene Index Number, we should determine what the current scene is.
Let's Refactor again to make our code more readable:
Let's Refactor again to make our code more readable:
Managing Multiple Levels
Managing Multiple Levels
Our aim is to have multiple levels.
Our aim is to have multiple levels.
When you reach the end of level one, it moves to level two and so on.
When you reach the end of level one, it moves to level two and so on.
If we reach the end of all the levels we should restart from level one.
If we reach the end of all the levels we should restart from level one.
Create a new level (duplicate)
Create a new level (duplicate)
Re-arrange the obstacles to make it different.
Re-arrange the obstacles to make it different.
Save your scenes with unique names.
Save your scenes with unique names.
Return to build settings.
Return to build settings.
Add the new scene to the build.
Add the new scene to the build.
Take note that you can reorder your scenes here, just drag and drop.
Adjust the Switch Statement.
Adjust the Switch Statement.
When we hit the landing pad we should load the next level.
When we hit the landing pad we should load the next level.
Create a method: LoadNextLevel
Create a method: LoadNextLevel
Ensure that when we reach the final level we reload the very first level:
Ensure that when we reach the final level we reload the very first level:
Currently Scenes reload INSTANTLY.
Currently Scenes reload INSTANTLY.
We are going to add a DELAY.
We are going to add a DELAY.
This is done using the INVOKE method.
This is done using the INVOKE method.
Now there is a slight delay after the level loads.
Now there is a slight delay after the level loads.
REFACTOR:
REFACTOR:
We should have a crash sequence method.
We should have a crash sequence method.
This way we can do multiple things when the player crashes.
This way we can do multiple things when the player crashes.
We can disable the controls.
We can disable the controls.
Play a sound effect.
Play a sound effect.
Add a particle effect.
Add a particle effect.
We have successfully disabled the movement and waited for a moment before reloading the level.
We will come back to our Crash Sequence.
We will come back to our Crash Sequence.
Let's move on to what happens when we successfully land on the landing pad.
Let's move on to what happens when we successfully land on the landing pad.
Working with multiple audio clips.
Working with multiple audio clips.
Set the AudioClip for the Rocket to None.
Set the AudioClip for the Rocket to None.
We will programatically apply the correct clip
We will programatically apply the correct clip
Change the Movement Script:
Change the Movement Script:
Add an AudioClip variable to store our SFX.
Add an AudioClip variable to store our SFX.
Read about the PlayOneShot method in the Unity Documentation.
Read about the PlayOneShot method in the Unity Documentation.
Start in the Collision Handler
Start in the Collision Handler
State Tracking
State Tracking
There is a moment when we are waiting for the level to reload.
There is a moment when we are waiting for the level to reload.
This is a transitional state.
This is a transitional state.
There are things that cannot happen during transition:
There are things that cannot happen during transition:
Don't play any more collision audio
Don't play any more collision audio
Don't allow the player to control the rocket
Don't allow the player to control the rocket
Don't start the success sequence if you have crashed.
Don't start the success sequence if you have crashed.
Class Structure
Class Structure
Note that our basic class structure is:
Note that our basic class structure is:
Serialized variables
Cached component references
State variables
Methods
When we collide
When we collide
Check if we are transitioning
Check if we are transitioning
If we are, exit (return) from the method.
If we are, exit (return) from the method.
Else, determine the collision
Else, determine the collision
Act accordingly.
Act accordingly.
Set the transisiton state to true at success and fail points.
Set the transisiton state to true at success and fail points.
Stop the audio source playing once transisiton state changes.
Stop the audio source playing once transisiton state changes.
Triggering Particle Effects
Triggering Particle Effects
We want to trigger a particle effect when we crash or reach the landing pad.
We want to trigger a particle effect when we crash or reach the landing pad.
Pre-made particle effects are provided for you below.
Pre-made particle effects are provided for you below.
Make your own effects if you prefer.
Make your own effects if you prefer.
Attach your particles to your Rocket Prefab.
Attach your particles to your Rocket Prefab.
Create references for our two particle systems:
Create references for our two particle systems:
Add your particle systems to the serialized fields.
Add your particle systems to the serialized fields.
Practice using particle systems:
Practice using particle systems:
Add a particle effect for thrusting.
Add a particle effect for thrusting.
Refactoring code with EXTRACT
Refactoring code with EXTRACT
Refactor for clarity!
Refactor for clarity!
Highlight all code in IF-Statement
Highlight all code in IF-Statement
Now press CTRL+FULL-STOP
Now press CTRL+FULL-STOP
Choose EXTRACT METHOD
Choose EXTRACT METHOD
Press F2 to rename the method
Press F2 to rename the method
Setup Controls for Testing
Setup Controls for Testing
World Building
World Building
Time to enhance our levels using simple shapes.
Time to enhance our levels using simple shapes.