Raycasters

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.

Using Raycasters to change states

Adding Raycasters

Adding a Raycaster

To add a raycaster, you can use the following instructions:

  1. 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
  2. Find the Raycaster component and add it to your Hierarchy

Using Raycasters

To specify which Node(s) in your scene the raycaster will interact with:

  1. Assign a Tag to the Raycaster’s SearchTags property.
  2. Assign the same tag to the Node(s) you wish the Raycaster to interact with.

Adding Search Tags in Raycasters Node Properties

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.

Adding a tag to a Node

Intersection Events

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 SearchTag value. Mattercraft provides three key intersection events:

Event Description
onIntersection Fired while the raycaster is currently intersecting with a node with the same tag
onIntersectionEnter Fired when the raycaster intersects / enters a node with the same tag
onIntersectionLeave Fired when the raycaster stops intersecting with a node with the same tag

See Mattercraft's full API documentation here.

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.

Using onIntersectionEnter Event in a Behavior Action to change states

If you want to test your intersection events within the editor, make sure you tick the Run At Edit Time checkbox within the selected behavior actions properties.

Use Cases

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.

Use Cases Description
Selecting Objects Raycasters can be used to highlight objects or nodes in your 3D scene and trigger specific actions based off the selection.
Initiating Action 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.
Navigation 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.
Object Manipulation 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.
zapcode branded_zapcode i