Shooting Objects from the Camera Viewpoint
-
Introduction
In “first-person shooting” type of games, we need to shoot objects like bullets or arrows from the camera’s viewpoint. This is especially useful when you are using a first person view camera.
This article will introduce 3 blocks that will help you do that.
Copy Camera Direction
When an object is added to the scene, it faces the same direction as the Y-axis. We can use the following block to make the object face the same direction as the camera:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/eb283084-c8db-41dd-85fe-0a25190c6da6.png" width="300" style="">
For example, let’s add a box that points forward. Whenever you click the “copy direction from camera” block, the box will rotate to match the direction of the camera:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/101c9b18-0ff9-4bd1-8de9-7c2edc1b475c.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">
Copy Camera Position
Similarly, we can also move the object to the same position as the camera, using the “copy position from camera” block:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/99d505fc-f2d9-4b38-b2fd-10b018b63449.png" width="300" style="">
For example, if we run the “copy position” block, the box will move to the same position as the camera. You won’t see the box anymore, because now the camera is inside that box. So you need to zoom out the camera to see it.https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/1027a62e-c463-41f7-965a-c46a16fa0ffb.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">
Move Object in Current Direction
Lastly, to shoot this object out from its current position and direction, we can use the “set speed” block, and select “Directional Speed”. The directional speed is the speed that the object moves in its current facing direction.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ead88f80-64d8-4cb0-a59c-96d0043fbd10.png" width="350" style="">
For a demo, in the program below, we keep on adding new boxes and shooting them out every 0.5 seconds:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9fceb806-0b3e-4997-9e1f-e4eda8af9c76.png" width="500" style="">
When we run the program, new boxes are shooting out.https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/21a646ad-f0ae-45c3-910e-aa52bc93c6dd.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">
Note that the boxes will not move below the ground when we set their directional speeds. If you would like the boxes to keep moving down, you need to set a much higher target point for the camera (such as Z = 10000).You can play with this demo project here:
https://play.creaticode.com/projects/377563a0134456c0ce412fc6
-
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