Rocket Boost

Game Design

The player will need to fly a rocket.

From one side of the screen to the other.

Move using boosters. 

Avoid obstacles along the way.

Level Design

Let's create the: GROUND

Add a Cube Game Object.

Make sure it is at the center of the world (0,0,0)

Change it's position and scale as shown in the screenshot.

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.

Add materials for each new Game Object to distinguish them.

Make each Game Object you have created a Prefab.

Prefabbing objects has many advantages:

Your scene should look similar to this at the end.

Create a new C# Script and call it Movement

Attach the Script to the Rocket.

At this point we should organise our project into folders for Prefabs, Materials and Scripts.

Controlling the Rocket

We will apply forces to the RigidBody of the Rocket:

Let's test the thruster:

Rotating the Rocket

Code refactor point

Computer programmers and software developers refactor code to 

improve the design, structure and implementation of software. 

Refactoring improves code readability and reduces complexities. 

Working with Prefabs

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.

We need to override the Prefab with all of our changes.

Add an obstacle to the level.

Make sure it has a material.

Make it a Prefab.

Freeze the Rocket's Z position.

Now Rocket can only travel on the X and Y planes.

Freeze the X and Y Rotation too.

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.

Let's fix this:

If you want to make the movement more realistic, add some Drag to the Rigidbody.

AUDIO

Audio in Unity requires 3 things:

The Main Camera already has an AudioListener on it.

Add an AudioSource to the Rocket.

Below is the Audio File for the Rocket.

Drag the Audio File to the AudioClip in the AudiSource.

Testing using Tags and Switch / Case Statements

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.)

Create a new C# Script to handle our collisions.

Call it CollisionHandler.

Attach it to the Rocket.

Create the OnCollisionEnter Method.

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

This game has instant death.

If our Rocket hits any obstacle it explodes.

We need to Respawn the rocket to try again.

We do this by reloading the scene using SceneManager.

Go to File --> Build Settings

We need to add our Scene to the build.

As it is currently empty.

Once you have added it, note that it can also be referenced by its index number.

We first add SceneManagement to the NameSpace.

Create a method to Reload your Scene.

Call it as the Default Case.

It's bad practice to hardcode.

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:

Managing 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.

If we reach the end of all the levels we should restart from level one.

Create a new level (duplicate)

Re-arrange the obstacles to make it different.

Save your scenes with unique names.

Return to build settings.

Add the new scene to the build.

Take note that you can reorder your scenes here, just drag and drop.

Adjust the Switch Statement.

When we hit the landing pad we should load the next level.

Create a method: LoadNextLevel

Ensure that when we reach the final level we reload the very first level:

Currently Scenes reload INSTANTLY.

We are going to add a DELAY.

This is done using the INVOKE method.

Now there is a slight delay after the level loads.

REFACTOR:

We should have a crash sequence method.

This way we can do multiple things when the player crashes.

We can disable the controls.

Play a sound 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.

Let's move on to what happens when we successfully land on the landing pad.

Working with multiple audio clips.

Set the AudioClip for the Rocket to None.

We will programatically apply the correct clip

Change the Movement Script:

Add an AudioClip variable to store our SFX.

Read about the PlayOneShot method in the Unity Documentation.

Start in the Collision Handler

State Tracking

There is a moment when we are waiting for the level to reload.

This is a transitional state.

There are things that cannot happen during transition:

Don't play any more collision audio

Don't allow the player to control the rocket

Don't start the success sequence if you have crashed.

Class Structure

Note that our basic class structure is:

When we collide

Check if we are transitioning

If we are, exit (return) from the method.

Else, determine the collision

Act accordingly.

Set the transisiton state to true at success and fail points.

Stop the audio source playing once transisiton state changes.

Triggering Particle Effects

We want to trigger a particle effect when we crash or reach the landing pad.

Pre-made particle effects are provided for you below.

Make your own effects if you prefer.

Attach your particles to your Rocket Prefab.

Create references for our two particle systems:

Add your particle systems to the serialized fields.

Practice using particle systems:

Add a particle effect for thrusting.

Refactoring code with EXTRACT

Refactor for clarity!

Highlight all code in IF-Statement

Now press CTRL+FULL-STOP

Choose EXTRACT METHOD

Press F2 to rename the method

Setup Controls for Testing

World Building

Time to enhance our levels using simple shapes.