3D - A Flying Drone (Difficulty: 1)
-
Key Topics Covered
Introduction
In this tutorial, we’ll fly a drone across a scene:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/dd23f241-bc95-4cbe-afe3-3a2d525db267.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
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.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/e7d7f153-cc0d-4534-97b0-2c7f92cdb1f1.png" width="480" style="">
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.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/68edd2dc-a9d0-478d-9c80-f0907ce07ea4.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">
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.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/f2856b6f-1f5e-4ad4-9a12-3e27a74bfe1d.png" width="1000" style="filter: drop-shadow(0.35rem 0.35rem 0.4rem rgba(0, 0, 0, 0.5));">
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.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/231290d2-0ac3-4c88-a559-257c9a39adcc.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">
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.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b96cea20-32be-4c72-b7a6-2587ad3801b2.png" width="350" style="">
Here is what you would get when pressing the “e” key:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/80b6bace-3836-45dd-acdf-e9ae45614968.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">
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?
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/d8d8415a-fba7-4c12-bc13-d4a637d9619b.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
Here is how to modify the program:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/de27c81f-fe2d-43fd-b641-04f8cc76d319.gif" width="800" style="border-radius: 5px; border: 1px solid #29622d;">
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:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/025e6399-8d14-4f60-a5a8-d30421a4b95a.png" width="400" style="">
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.https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b9d509be-e477-4704-8873-74c079f9592f.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
Step 8 - Turn right on the “d” key
Next, can you try to make the drone turn right when the “d” key is pressed?
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/383df6bb-67e4-4c7f-be8f-23d1a5197b45.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
Here is one way to do it. We just need to set its “z rotation” to 100 instead of -100.https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9e818230-a580-4df2-ae79-3bbb0dd13ccd.png" width="400" style="">
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.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/dd23f241-bc95-4cbe-afe3-3a2d525db267.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
Here is how to add the new blocks:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ade1cce0-4864-42c5-b020-2e3770cb6559.gif" width="800" style="border-radius: 5px; border: 1px solid #29622d;">
Here is the final program for your reference.https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/90ee5b04-f809-48c2-a992-c906da44a7b3.png" width="1000" style="filter: drop-shadow(0.35rem 0.35rem 0.4rem rgba(0, 0, 0, 0.5));">
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b081e99f-8fab-488a-8bdf-f2791e90cc4f.png" width="350" style="filter: drop-shadow(0.35rem 0.35rem 0.4rem rgba(0, 0, 0, 0.5));">
Creative Ideas
There are many ways you can extend this project. Here are some ideas for your inspiration:
-
I CreatiCode pinned this topic on
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login