Raycasting is a process that involves shooting a line (or ray) into 3D space from an origin point and determining the node(s) it intersects with. This is useful for detecting nodes in 3D space and interacting with them in a more dynamic way.
Essentially, Raycasters act as pointers that allow you to interact with objects in the scene. Firing raycasters from a camera (AR) or controllers (in VR mode, WebXR) can help setup user interaction without the need for a click or user tap.
To add a raycaster, you can use the following instructions:
- Make sure nothing is selected in your Hierarchy Panel and click the + (plus) icon at the top of the panel, or right-click on Group in the Hierarchy Panel and click the three dots next to it
- Find the Raycaster component and add it to your Hierarchy
To specify which Node(s) in your scene the raycaster will interact with:
- Assign a Tag to the Raycaster’s
- Assign the same tag to the Node(s) you wish the Raycaster to interact with.
You can add as many tags as you want to this property. The corresponding nodes that have those tags will be the only nodes the raycaster will interact with.
Tags are not only used for specifying which node the raycaster will interact with, they are also useful in categorizing the interactions. So it's recommended to make them descriptive and applicable to their use case.
For example, the video at the beginning of this article shows different States being activated based on the sphere the raycaster interacts with. Each sphere corresponds to a color, and it changes to that color when the raycaster intersects with it.
When Mattercraft detects an intersection between a raycaster and node in 3D space, It provides detailed information about the intersection; including the point of intersection, the normal at the intersection point, and the UV coordinates.
An intersection event is an event that is fired as a result of a raycaster intersecting with a node that includes the same
tag value as the raycaster's
Mattercraft provides three key intersection events:
|Fired while the raycaster is currently intersecting with a node with the same tag
|Fired when the raycaster intersects / enters a node with the same tag
|Fired when the raycaster stops intersecting with a node with the same tag
You can use these intersection events within scripts to create complex interactions. Alternatively, you can easily use them with Behavior Actions. To do so, add an action from the Behaviors Panel and select the desired intersection event within its event property.
If you want to test your intersection events within the editor, make sure you tick the
Run At Edit Timecheckbox within the selected behavior actions properties.
There are a variety of innovative ways you can use raycasters in your immersive experiences, from a simple 3D web experience to a VR one. Here are just a few.
|Raycasters can be used to highlight objects or nodes in your 3D scene and trigger specific actions based off the selection.
|Raycasters can also be used to initiate actions, such as simulating a button press or activating a specific behavior.
|Interacting with UI
|Raycasters can be used to interact with user interface elements in a 3D space. By detecting the intersection with UI elements, such as buttons or menus, you can enable user interaction and trigger corresponding events.
|Raycasters can be utilized for navigation purposes. By detecting intersections with navigational elements, such as waypoints or teleportation points, you can enable users to navigate within the 3D environment just by pointing the camera in that direction.
|Raycasters can facilitate object manipulation in a 3D scene. By detecting intersections with interactive objects, you can enable users to grab, move, rotate, or scale objects using controllers or gestures.