Shooting Objects from the Camera Viewpoint
CreatiCode last edited by info-creaticode
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.
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:
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:
Similarly, we can also move the object to the same position as the camera, using the “copy position from camera” block:
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.
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.
For a demo, in the program below, we keep on adding new boxes and shooting them out every 0.5 seconds:
When we run the program, new boxes are shooting out.
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: