Picking Events
-
Prerequisite
Introduction
To allow the user to interact with objects in the scene, we can enable the “picking” event. It is very similar to the “click” event in 2D projects. When you click on a 3D object in the scene, the picking event will be triggered.
Note that the event will be triggered when you release the pointer, not when you press down the pointer.
The “Turn On Picking” Block
The picking event is not enabled by default because it requires some extra computations. To enable it, you can use the “turn on picking” block:
Parameters
Mouse Button
You can choose to make the user pick using the left or the right mouse button. This choice should not conflict with the navigation button when you are using the follow camera’s free mode. For example, if the user uses the right button to control the follow camera, then he should use the left button to pick objects.
Sprite List
To specify which objects in the scene can be picked by the user, you can specify a list of sprite names separated by commas, such as “Sprite1,Sprite2”. This way, any object added in one of those 2 sprites will be pickable. If this input is blank, then all objects in the current sprite will become pickable.
The “Turn Off Picking” Block
Sometimes you may need to disable the picking event after it has been turned on. You can use the “turn off picking” block:
It takes only one input, which is the list of sprite names. These names should be separated by commas, such as “Sprite1,Sprite2”.
The “When Picked” Block
Whenever the user picks an object, this event block will be triggered if it is used in the sprite that has added that object.
Blocks Describing the Picking Event
When we pick on an object, we are shooting out a light ray from our camera to the mouse pointer, and then extending this ray until it hits any object in the scene. The intersection point between this light ray and the target object is called the “picked point”, and the target object is called the “Picked Object”.
The following 4 blocks are available for you to get more information about the picking event.
Demo
In this demo project, there are 2 sprites.
In “Sprite1”, we add a yellow box to serve as the ground, then enable user picking for any objects created in the “Boxes” sprite. Then we send a message to add boxes.
In the “Boxes” sprite, when we receive the message, we add 10 boxes at random positions. Next, when any box is clicked, we first select that box using “picked object name”, then we set its rising speed to make it fly.
Try it yourself here:
https://play.creaticode.com/projects/e09563dbcf2abc29327819bd
-