Rocket Launch
Rocket Launch
Beginner Curriculum
Project 2:
Rocket Launch
Project 2:
Rocket Launch
Objective
In this guide we are going to learn about the fundamentals of programming, while coding a
rocket launch in a space environment, and learn to control the movement of a an object using
keyboard buttons.
You will code a game where you are able to make a rocket fly up and down by pressing the up,
and down arrow keys and stop by pressing the space key.
Concepts covered:
Motion in 3D Space
introduction to variables
https://kids.hatchxr.com/@XR4schools/G25-FC-S2
https://kids.hatchxr.com/@XR4schools/G25-FC-S2-template
28
How it works?
https://kids.hatchxr.com/@XR4schools/G25-FC-S2
The moment you click on the “Green Play Button”, you will notice
2. The rocket stops moving when you press the space key on the keyboard
3. Pressing the up and down arrow keys makes the rocket move up and down
4. When the rocket is moving, flames are blowing from the bottom of the rocket, and when
the rocket stops, the flames stop as well.
89
Objective No. 1: Understanding the template
Step 1: We will start by opening the template link of the project mentioned here.
Step 2: The link above will open up an empty project with no code in the hatch workspace,
On the left panel There is an object by the name 3D object by the name
you will see the list “ParticleSystem” that is not visible “Rocketship”, for which
of objects added in initially, but this will help us add you will code motion
the game flames at the end of the rocket with keyboard controls
As you can see, the code window of the hatch workspace opens up, and this is where you can
30
Objective No. 2: Making the rocket fly up
Let’s start by making the rocketship move up, when the green play button is clicked, just like
Step 1: Click on name “Rocketship” in the left panel. You will see a list of blocks appear, that
shows all the activity you can perform on the rocketship object
Step 2: There is a block named “Rocketship move forward for 5 meters”. Click on the block
31
We know that the rocket will not move up on its own, it will need an event block, for the
computer to know when to move the rocket up.
Step 3. Click on the Events section in the top section of the left panel on your screen.
Step 4: You will see a list of yellow colored event blocks appear. Lets click and drag out the very
first block that says, “When green play button clicked”
32
Sep 5: Drag the “move forward” block and attach it inside the “when green play button
clicked” block.
Click on the green play button, and you will see the rocketship move in its forward direction.
We want to make the rocket move in the upward direction.
First, click on the reload button to reset the scene and bring the rocket back to the moon
Step 6: You will notice that in the “move forward” block, there is a drop down menu option
with the text “move forward”. Click on it, and a menu appears that lets you select different
directions in which you can make the rocket move.
33
Step 7: Select the “move up” option from the drop down menu. The code in your workspace
would now be as shown here.
Click on the green play button, and now you will see the rocket fly up for some distance and
then stop.
Tidbit #1 : Try selecting each of the option for different movement direction (forward,
backward, left. right, up and down) and run the code for each case to see how they affect the
motion of the rocket.
Our next step is to make the rocket move up continuously, as long as we want without the
rocket stopping.
34
Objective No. 3: Using loops for repeated motion
We were able to make the rocket move up for a some distance. Let’s now try to make the
rocket move up without stopping. One way to do it would be to add multiple move up blocks
Try this, you will notice there are two main issues with this way of coding:
1. The rocket will eventually stop moving after all the move up blocks have been executed
2. Coding is all about efficiency, about performing activities in a simple way, and this is clearly
not efficient.
Well our idea of running the move up block again and again is correct, but the way of
To make the rocket move up on its own, we are telling the computer, to move it up for 5
So whenever we want to perform a same set of activity multiple times, we make use of loops
35
Step 0: Let’s first delete all the extra move up blocks that we have added except for one. To
delete any block, you can right click on the block, and a list of options will appear. There will be
an option called “Delete”, just click on it and your selected block will be removed from the
workspace.
Alternatively, you can click on a block and press the delete key on the keyboard to delete a
block as well.
Delete all the extra move up block leaving one, so that in your code window the following
blocks remain.
36
Step 1: In the top half of the left panel, you will notice “Loops & Timers” mentioned just below
“Events”. Click on “Loops & Timers”, you will see a list of new blocks appear.
In the loops section, you will notice a block titled, “repeat 10 times”. Click and drag it into the
code workspace.
Loops tell your computer to perform a set of activities multiple number of times.
37
Step 2: Inside the “repeat 10 times” block, you need to define the activity that you want to
repeat through your code. That would be the rocket move up action.
Click and drag the “rocket move up” block, inside the “repeat 10 times” block as shown here.
Step 3: You can now drag the “repeat 10 times” block and attach it inside the “when green
play button clicked” block.
38
Let’s run this code and see what happens. Click on the green play button.
You will notice, that the rocket, instead of just moving 5 meters up, keeps on moving up for
sometime. In fact the rocket ends up moving 50 meters up above the moon. That’s because,
the repeat block is going to run the activity of moving the rocket five meters up 10 times.
But this still has an issue, we want the rocket to keep on moving up and not stop at all. Here
Step 4: Click on the “Loops & Timers” on the left panel, and there is a block called “forever”.
39
Step 5: Click an drag the “forever” block into the workspace.
Step 6: Drag the “move up” block and place it inside the “forever” block.
40
Step 7: Delete the “repeat” block from the workspace. and drag and attach the “forever”
block inside the “green play button” clicked block.
Now when you click on the green play button to run the code, you will see that the rocket
keeps on moving up forever and does not stop.
Forever block is used to run a set of activity again and again indefinitely.
Click on the “reload” button to stop the rocket from moving and to bring it back to the moon.
41
Objective No. 4: Adjust the speed of the rocket with a variable
In the code that you have implemented, it says “Rocket move up for 5 meters”.
Try changing the number 5 to different values and you will see that if you reduce the value, the
rocket moves slowly, and if you increase the value then the rocket starts moving faster.
So the numerical value inside the move up block is controlling the speed of the rocket.
We can use a variable to change the value of speed of the rocket while playing the game.
Step 1: Click on the “variables” option in the top half of the left panel on your screen.
42
A window will appear on your screen and it will ask you to give a name to the variable you
want to create.
Since the variable we want to create is going to control the speed of the rocket, so we can
name the variable “speed”.
Step 3: Type the word “speed” in the box and click on the “create” button.
Step 4: Click and drag the “set variable speed to” block in the workspace
43
We need to give a value to the variable speed.
Step 5: On the left panel, click on the category “Math”, and a list of math blocks will appear. At
the top of the list, you will find a number block, that just says “0”. Click and drag the “0” block
44
Step 6: Change the value “0” to “5” inside the set variable speed block. Now that we have
created a variable and given it an initial value, we need to use the variable to move the rocket.
Step 7: Delete the number 5 block in the “Rocketship move up for 5 meters”.
Step 8: Click on the variables option in the left panel and select the “speed” block.
Step 9: Attach the “speed” block inside the “Rocketship move up” block.
45
Click on the green play button and you will notice that the rocket keeps on moving up.
Now, try changing the value of the speed variable in the “set variable speed to 5” block.
And upon running the code you will see, changing the value of the speed variable changes
With the motion of rocketship implemented, lets now learn to control the motion of the
Just like clicking on the green play button is considered as an event in the code, similarly, any
Step 1: Click on the “Events” section in the left panel of your screen.
Scroll down in the list of blocks that appear, and you will see a block that says:
Step 2: Click and drag the “When space key pressed” block in the workspace.
You will notice, the shape of “when space key pressed” block is the same as “when green
play button clicked” block. You can define a set of actions inside the when space key block,
and those actions will only happen in the game when you press the space key on your
keyboard.
46
We need to define what needs to happen in the game when the space key is pressed.
Let’s make the rocket stop moving when you press the space key.
How do we make an object stop moving? We can control the speed of the rocket, and if an
47
So, if we want the rocket to stop when you press the space, key,we need to tell the computer to
set the value of the speed of the rocket to “0” when the space key is pressed.
Step 3: Right click on the “set variable speed to 5” block. A drop down menu will appear,
Step 4: You will see another “set variable speed to 5” block appear in the workspace. Drag that
block and attach it inside the “when space key pressed” block.
The block would now say, “when space key pressed, set variable speed to 5”.
Because we want the rocket to stop when space key is pressed. its speed should become
48
Click on the green play button, and you will see the rocket fly up. Press the space key and you
will see the rocket stop moving.
Let’s make the rocket move up again when we press the up arrow key on the keyboard.
Step 5: Right click on the when space key pressed block in the workspace and select
“duplicate”.
A new “when space key pressed” block will appear in the workspace.
49
Step 6: Click on the word “space” in one of the “when space key pressed” block, and you will
see a drop down with the list of all the keyboard buttons. Select the “up arrow” option.
Your block would now say “when up arrow key pressed, set variable speed to 0”.
50
Click on the reload button to restart your game and then click on the green play button to run
the new code that you have in your workspace. You will see the rocket fly up.
Press the space key and you will see the rocket stop moving.
Now if you press the up arrow key, the rocket starts moving up again.
You can duplicate the “when space key pressed” block again, this time changing the “space”
key to “down arrow” key and change the number “0” to “-5”.
Now when you reload your scene and run the run the code, you will be able to move the rocket
We are almost done with this project, lets just add some special effect of flames blowing out of
the bottom of the rocket as it moves up or down, and then you can publish and share this
51
Objective No. 6: Add fire using particle system
In the bottom half of the left panel you will see there is an object named, “ParticleSystem”.
We will explore particle system in detail in future projects, for now we are just going to use the
Step 1: Click on the name “ParticleSystem”. A list of blocks will appear. Scroll down and you
Step 2: Drag the “turn emitter on” block and attach it above the “forever” block inside the
52
Step 2: Bring our another“turn emitter on” block and attach it in the “when up arrow key
pressed” block. And another one inside the “when down arrow key pressed” block.
Step 3: Bring out on last “turn emitter on” block and attach it in the “when space key
pressed” block. Click on the drop down menu option, and select the “turn emitter off” option
in this.
53
This is what the final code of your project should look like.
With the turn emitter on / off blocks we are telling our computer to add the flame when rocket
flies up, and when it moves down, and then saying that when rocket stops moving, turn off the
flames.
Click on the green play button and you will be able to control the motion of your rocket using
your keyboard button.
Your project is ready. Login to your account, give your project a name and publish it by
clicking on the “REMIX” button at the top left corner of the screen and then you will be able
to share it with your friends.
END OF GUIDE
54