# Hit testing sample

This sample demonstrates how to hit test against points and planes found in the real world.

For basic information about hit testing using raycasts and what AR Foundation’s `AR Raycast Subsystem` component does, see the [Unity documentation](https://docs.unity3d.com/Packages/com.unity.xr.arfoundation&#64;latest/manual/features/raycasts.html). To use this feature, enable it in the OpenXR plugin settings located under `Project Settings > XR Plug-in Management > OpenXR (> Android Tab)`. The wordings **ray casting** and **hit testing** are used interchangeably throughout this topic. For more information, see [Hit Testing Feature](https://docs.qualcomm.com/doc/80-88642-11/topic/features_hittesting.html#hit-testing).

## How the hit testing sample works

Adding the `AR Raycast Manager` to the `XR Origin` GameObject enables the ray casting subsystem. The `AR Raycast Manager` component provides a field for defining a prefab to spawn upon successfully intersecting with a plane. For this sample, the prefab has been left blank, as this functionality is implemented in the `Hit Testing Sample Controller` component.

The raycast manager’s `Raycast` function returns a boolean value depending on whether the ray has hit a plane or not. The hit result(s) are added to the ARRaycastHit list, sorted by ascending distance (closest first).

public void Update() {
        CastRay();
        _activeIndicator.transform.position = _desiredPosition;
    }
    
    private void CastRay() {
        Ray ray = new Ray(_cameraTransform.position, _cameraTransform.forward);
        List<ARRaycastHit> hitResults = new List<ARRaycastHit>();
        if (_raycastManager.Raycast(ray, hitResults)) {
            _desiredPosition = hitResults[0].pose.position;
            ...
        }
        else {
            ...
        }
    }
    Copy to clipboard

The sample scene has a default red colored gizmo indicator that follows the controllers. For every frame, a raycast is created from the origin of controller. If the raycast hits, the indicator gizmo changes color and moves to the hit position. If it does not hit anything, the indicator gizmo remains the same.

Last Published: Jun 06, 2025

[Previous Topic
Camera frame access sample](https://docs.qualcomm.com/bundle/publicresource/80-88642-11/topics/samples_cameraframeaccess.md) [Next Topic
Image tracking sample](https://docs.qualcomm.com/bundle/publicresource/80-88642-11/topics/samples_imagetracking.md)