AR Raycast Manager component
The ARRaycast
AR Raycast Manager component
Ray casting
Ray casting (also known as hit testing) allows you to determine where a ray (defined by an origin and direction) intersects with a trackable. The ray cast interface is similar to the one in the Unity Physics module, but since AR trackables don't necessarily have a presence in the physics world, AR Foundation provides a separate interface.
The Raycast Manager serves two purposes:
- Provides an API to perform single raycasts.
- Allows you to create a persistent ARRaycast. An
ARRaycast
is a type of trackable and is updated automatically until you remove it. Conceptually, it is similar to repeating the same raycast query each frame, but platforms with direct support for this feature can provide better results.
Single raycasts
There are two ray casting methods on the ARRaycast
The first method takes a two-dimensional pixel position on the screen.
public bool Raycast(
Vector2 screenPoint,
List<ARRaycastHit> hitResults,
TrackableType trackableTypes = TrackableType.AllTypes)
You can, for example, pass a touch position directly:
[SerializeField]
ARRaycastManager m_RaycastManager;
List<ARRaycastHit> m_Hits = new List<ARRaycastHit>();
void OnEnable()
{
InputSystem.EnhancedTouch.EnhancedTouchSupport.Enable();
}
void OnDisable()
{
InputSystem.EnhancedTouch.EnhancedTouchSupport.Disable();
}
void Update()
{
var activeTouches = InputSystem.EnhancedTouch.Touch.activeTouches;
if (activeTouches.Count == 0)
return;
if (m_RaycastManager.Raycast(activeTouches[0].screenPosition, m_Hits))
{
// Only returns true if there is at least one hit
}
}
The second method takes an arbitrary Ray (a position and direction):
public bool Raycast(
Ray ray,
List<ARRaycastHit> hitResults,
TrackableType trackableTypes = TrackableType.AllTypes)
The following table summarizes the other parameters:
Parameter | Description |
---|---|
hitResults |
The results for both methods are stored in this List , which must not be null . This lets you reuse the same List object to avoid garbage-collected allocations. |
trackableTypeMask |
The type, or types, of trackables to hit test against. This is a flag, so multiple types can be bitwise OR'd together, for example, Trackable |
Determining what the raycast hit
If the raycast hits something, hitResults
will be populated with a List
of ARRaycast
Use the hit
void HandleRaycast(ARRaycastHit hit)
{
if (hit.trackable is ARPlane plane)
{
// Do something with 'plane':
Debug.Log($"Hit a plane with alignment {plane.alignment}");
}
else
{
// What type of thing did we hit?
Debug.Log($"Raycast hit a {hit.hitType}");
}
}
Persistent raycasts
Persistent raycasts are a type of trackable. Each ARRaycast continues to update automatically until you remove it or disable the ARRaycast
To add or remove a persistent raycast, call Add
Persistent raycasts must be created from a screen point:
public ARRaycast AddRaycast(Vector2 screenPoint, float estimatedDistance)
When you create a new ARRaycast, AR Foundation creates a new GameObject with an AR Raycast component on it. You can optionally provide a Prefab in the Raycast Prefab field that is instantiated for each ARRaycast, which allows you to extend the default behavior of each ARRaycast.
Supported trackables
ARRaycast
TrackableType | ARCore | ARKit |
---|---|---|
Depth |
✓ | |
Face |
||
FeaturePoint |
✓ | ✓ |
Image |
✓ | |
Planes |
✓ | ✓ |
PlaneEstimated |
✓ | ✓ |
PlaneWithinBounds |
✓ | ✓ |
PlaneWithinInfinity |
✓ | |
PlaneWithinPolygon |
✓ | ✓ |