3D Escape Game: Rocket Escape

Emre Baskak
4 min readJan 3, 2023

--

Game no 7: Rocket Escape!

3D Escape Game: Rocket Escape

Please click the link below to play the game on the web:

3D Escape Game: Rocket Escape

Rocket Escape is a 3D game that I created using #unity and C#.

The goal is to move the rocket from the launching pad to the landing pad. While flying in space, you should avoid touching any obstacles on the way. Press Space to launch, and rotate your rocket with the right and left arrow keys.

I’ll be writing some of the key methods and game mechanics that I learned to create this game.

Input.GetKey()

There are two ways we can use Input.GetKey() method. Please check the codes below and realize the difference. Keep in mind that both of these codes do the same job.

Input.GetKey() — 1
Input.GetKey() — 2

AddRelativeForce()

It is used to add a force to the Rigidbody in the direction specified by the function’s parameters.

AddRelativeForce()

P.S. Vector3.up is an equivalent of 0, 1, 0 that moves out the object on the y-axis.

Catching A Reference To A Component

When we want to control any component of an object through a script, we need to catch a reference to that specific component in that script.

Let’s say that I want to turn on the Gravity in Rigidbody component when the player presses the Space button on the keyboard.

First of all, I declare a Rigidbody variable. Then, I catch a reference to that variable in the Start() function, so that we can use that variable from the moment game starts. And finally, when the desired conditions are met, we can turn on gravity.

P.S. Gravity field in Rigidbody seems as “Use Gravity”. When we use that field in our script, we use it as useGravity. Please check the code below.

Catching A Reference To A Component

Drag Field in Rigidbody

The drag force acts opposite to the direction of object’s movement. In Unity, it determines how much the object resists movement due to air resistance and friction. So, it slows down the object.

Drag Field

In our game, I applied 0.25 drag force to the rocket to give a realistic feeling as the rocket moves through the air.

isPlaying

It is a property that returns a boolean value that shows whether the game is currently being played or not.

isPlaying

We can also use isPlaying property to check if a sound clip is being played or not. For example, the code below plays the audio if it isn’t already being played.

isPlaying Example

Switch Statements

Switch statements are like If/Else conditionals. You can use them when you want to compare a single variable to a series of constants. In other words, they help us to choose from a number of different actions based on the value of a given expression.

Switch Statement

If the value of playerHealth doesn’t match any of the case values, the code in the “default” block will be executed.

Reloading A Scene

To reload a scene, we need to use SceneManagement namespace’s SceneManager.LoadScene() method. Inside the paranthesis we can either write the index of the scene or the name of the scene as a string.

Reloading A Scene

Let’s say that our game has thousands of scenes and if the player dies, he has to start on that exact level. In this scenario, we need to reload the level in that exact scene. To do that we can use SceneManager.GetActiveScene().buildIndex method which returns the index of the current scene.

SceneManager.GetActiveScene().buildIndex

For better readability we can write the same code as below:

Reloading The Scene

Loading The Next Scene

When we complete a level, we need to jump on to the next level. Which is very easy to do.

Loading The Next Level

P.S. SceneManager.sceneCountInBuildSettings returns the number of total scenes here.

Invoke()

When we complete a level, it would be better to wait for a little bit before we pass to another level. For that, we can use Invoke() method.

In the parentheses, first, you write the name of the method you want to the delta in quotation marks, then next to it, you write the delay time in seconds.

Difference Between AudioSource.Play() and AudioSource.PlayOneShot()

With AudioSource.PlayOneShot() we can specify which audio clip we want to play.

How to Quit The Game

To quit the game, you can use Application.Quit() method.

Application.Quit()

--

--