3D - A Flying Drone (Difficulty: 1)
-
Key Topics Covered
Introduction
In this tutorial, we’ll fly a drone across a scene:
Step 1 - Initialize the 3D Scene
Please create a new project, and remove the dog sprite. In the “Empty1” sprite, add the green-flag block and the “initialize 3D scene” block. Please also choose a scene with some objects in it, such as “Castle”, “City”, “Garden” or “Village”. Note that the “City” and “Village” scenes take a bit longer to load if you have a slow computer.
Step 2 - Add a Drone Model
You can add a drone model using the “add model” block. Click the “Please select” input to bring out the model library, then pick any model you like. Note that you need to choose a reasonable height for the drone, so that it is not too big or too small.
Note that it is faster to click this block to run it alone. If you attach it to the “initialize 3D scene” block and run them together, then it will take longer.
Step 3 - Add a “Follow” Camera
Since the drone will be flying around, we can not use a camera that stays at the same location. Instead, we need to use a “follow camera”, which follows a target object wherever it goes.
Note that you need to set the “direction lock” to “Target”, so that the camera will always face the same direction as the drone.
Step 4 - Make a New Block for “Handle Keys”
To keep all the key-handling logic in one place, let’s first make a new block called “handle keys”. You can pick a new color for your block as well.
Step 5 - Rise when the “e” key is pressed
Now let’s make the drone rise when the “e” key is pressed, and make it stop when no key is pressed. We need a “forever” loop to keep checking if the “e” key is pressed. To make the drone rise or stop, we can set the “rising speed” to 400 or 0.
Here is what you would get when pressing the “e” key:
Step 6 - Fall when the “q” key is pressed
Whenever the “q” key is pressed, we should set the “rising speed” to a negative number, so that the drone will descend down. Can you add that logic?
Here is how to modify the program:One of the benefits of using the “rising speed” is that the drone will never fall below the ground (Z position of 0). Please give it a try.
Step 7 - Turn the Drone Left on “a” key
Whenever the “a” key is pressed, we should turn the drone left by setting the “Z Rotation” speed. Similarly, when no key is pressed, we should reset the z rotation speed to 0:
Note that this new “if-else” branch should be stacked below the previous one, because the logic for handling rotation speed and rising speed do not depend on each other. For example, the drone can be rising and turning at the same time.
Step 8 - Turn right on the “d” key
Next, can you try to make the drone turn right when the “d” key is pressed?
Here is one way to do it. We just need to set its “z rotation” to 100 instead of -100.
Step 9 - “w” and “s” for going forward and backward
Lastly, we need to add 2 more keys: “w” for making the drone fly forward, and “s” for flying backward. You can set the “forward speed” to a positive or negative number.
Here is how to add the new blocks:
Here is the final program for your reference.
Creative Ideas
There are many ways you can extend this project. Here are some ideas for your inspiration:
-