Perform raycasts in your Unity (AR Foundation) app

Perform a raycast, or hit-test, to determine the correct placement of a 3D object in your scene. Correct placement ensures that the AR content is rendered at the appropriate (apparent) size.

Hit result types

A hit-test can yield four different types of hit results, as shown by the following table.

Hit result type Description Orientation Use case Method calls
Depth Uses depth information from the entire scene to determine a point’s correct depth and orientation Perpendicular to the 3D surface Place a virtual object on an arbitrary surface (not just on floors and walls) Depth must be enabled for this to work.

ARRaycastManager.Raycast(Vector2 screenPoint, List<ARRaycastHit> hitResults, TrackableType trackableTypes TrackableType.Depth)
Plane Hits horizontal and/or vertical surfaces to determine a point’s correct depth and orientation Perpendicular to the 3D surface Place an object on a plane (floor or wall) using the plane’s full geometry. Need correct scale immediately. Fallback for the Depth hit-test ARRaycastManager.Raycast(Vector2 screenPoint, List<ARRaycastHit> hitResults, TrackableType trackableTypes TrackableType.PlaneWithinPolygon)
Feature point Relies on visual features around the point of a user tap to determine a point’s correct position and orientation Perpendicular to the 3D surface Place an object on an arbitrary surface (not just on floors and walls) ARRaycastManager.Raycast(Vector2 screenPoint, List<ARRaycastHit> hitResults, TrackableType trackableTypes TrackableType.FeaturePoint)
Persistent Raycast (Instant Placement) Uses screen space to place content. Initially uses estimated depth provided by the app. Works instantly, but pose and actual depth will change once ARCore is able to determine actual scene geometry +Y pointing up, opposite to gravity Place an object on a plane (floor or wall) using the plane’s full geometry where fast placement is critical, and the experience can tolerate unknown initial depth and scale ARRaycastManager.AddRaycast(Vector2 screenPoint, float estimatedDistance)

Perform a standard raycast

Call ARRaycastManager.Raycast(Vector2, List<ARRaycastHit>, TrackableType) to perform a raycast (hit-test). ARRaycastManager supports all TrackableTypes.

var touch = Input.GetTouch(0);
if (touch.phase != TouchPhase.Began)
    return;

// Raycast against planes and feature points.
const TrackableType trackableTypes =
    TrackableType.FeaturePoint |
    TrackableType.PlaneWithinPolygon;
List<ARRaycastHit> hits = new List<ARRaycastHit>();
// Perform the raycast.
if (raycastManager.Raycast(touch.position, hits, trackableTypes))
{
    // Raycast hits are sorted by distance, so the first one will be the closest hit.
    var hit = hits[0];
    // Do something with hit.
}

Conduct a raycast using an arbitrary ray and direction

Raycasts are typically treated as rays from the device or device camera, but you can use Raycast(Ray, List<ARRaycastHit>, TrackableType) to conduct a raycast using an arbitrary ray in world space coordinates instead of a screen-space point.

Create an Anchor using an ARRaycastHit

Once you have a raycast hit, use GameObject.AddComponent<ARAnchor> to create an Anchor and add it as a component to your GameObject, using the raycast hit as input. The ARAnchor component will continuously update the game object's Transform, so that the game object remains attached to the underlying Trackable for the hit result.

Persistent raycasts (Instant Placement)

Persistent raycasts use a screen location, and an estimated distance to a surface as input to create a new ARRaycast to place AR content in your scene. In ARCore, this is known as Instant Placement.

Call ARRaycastManager.AddRaycast() to create an ARRaycast that continues to update automatically until you call ARRaycastManager.RemoveRaycast() or disable the ARRaycastManager.

Initially, the ARRaycast's distance will be the distance provided in the AddRaycast() call. Once ARCore has mapped the local geometry and found an intersecting plane, the distance is updated to an actual distance. The change in distance can affect the apparent "size" or perceived scale of the object.

What’s next