Files
adriadri6972 d3d9c5f833 upload project
2025-07-31 15:21:08 +02:00

3.2 KiB

uid
uid
arfoundation-raycasts-single

Single ray casts

Single ray casts occur once. Use single ray casts for discrete, one time user interactions with the environment. For example, placement of a virtual object on a detected surface to preview what it looks like in a user's room.

There are two Raycast methods on the ARRaycastManager that perform single ray casts. Refer to Optional feature platform support to check whether your target platform supports each type of ray casting method.

Method Description
Viewport based ray cast Casts a ray from a two-dimensional pixel position on the screen (a screen point).
World based ray cast Casts an arbitrary ray (a position and direction).

Viewport based ray cast

The viewport based Raycast method casts a ray from a two-dimensional pixel position on the screen:

[!code-csARRaycastManager_Raycast_screenPoint]

You can, for example, pass a touch position directly:

[!code-csraycast_using_touch]

World based ray cast

The world based Raycast method takes an arbitrary Ray (a position and direction):

[!code-csARRaycastManager_Raycast_ray]

Parameters

The following table describes the common parameters of the Raycast methods:

Parameter Description
hitResults Contents are replaced with the ray cast results, if successful. Results are sorted by distance in closest-first order.
trackableType (Optional) The types of trackables to cast against.

Determine what the ray cast hit

If the ray cast hits something, hitResults is populated with a List of ARRaycastHits.

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

[!code-csraycasthit_trackable]