docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    AR Raycast Manager component

    The ARRaycastManager is a type of trackable manager.

    AR Raycast Manager component
    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:

    1. Provides an API to perform single raycasts.
    2. 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 ARRaycastManager that perform single raycasts.

    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 Update()
    {
        if (Input.touchCount == 0)
            return;
    
        if (m_RaycastManager.Raycast(Input.GetTouch(0).position, 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, TrackableType.PlaneWithinPolygon | TrackableType.FeaturePoint

    Determining what the raycast hit

    If the raycast hits something, hitResults will be populated with a List of ARRaycastHits.

    Use the hitType to determine what kind of thing the raycast hit. If it hit a trackable, such as a plane, then the ARRaycastHit.trackable property can be cast to that type of trackable:

    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 ARRaycastManager.

    To add or remove a persistent raycast, call AddRaycast or RemoveRaycast on the ARRaycastManager component from script code.

    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

    ARRaycastManager supports ray casting against most TrackableTypes; however, not all platforms support all trackables. For a list of supported TrackableTypes per platform, see below.

    TrackableType ARCore ARKit
    Depth ✓
    Face
    FeaturePoint ✓ ✓
    Image ✓
    Planes ✓ ✓
    PlaneEstimated ✓ ✓
    PlaneWithinBounds ✓ ✓
    PlaneWithinInfinity ✓
    PlaneWithinPolygon ✓ ✓
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)