XR INTERACTION TOOLKIT | AR FOUNDATION | AR RAYCAST MANAGER | RAYCAST | AR PLACEMENT INTERACTABLE | XR INTERACTION MANAGER | AR GESTURE INTERACTOR
Unity AR Virtual Object Placement
Add Feature that Allows Users to Add/Place a Virtual Object on the Planes being Displayed
STEPS NEEDED
- ARRaycastManager Component | XROrigin GameObject
- AR Placement Interactable Component | *AR Placement Interactable GameObject
- AR Gesture Interactor Component | MainCamera Gameobject
STEP ONE — ARRaycastManager Component | XROrigin GameObject
XR ORIGIN (XR RIG) > Add Component > ARRaycastManager
The AR Raycast Manager provides two things:
— Provides an API to perform single raycasts (similar to Physics.Raycast)
— Enables the creation of a persistent ARRaycast.
Physics.Raycast
Casts a ray, from pointorigin
, in directiondirection
, of lengthmaxDistance
, against all colliders in the Scene.
Bool Returns true if the ray instersects with a collider withhitinfo
containing information about where the closest collider was hit.
PERSISTENT RAYCASTS in AR are continuously updating trackables managed by the ARRaycastManager. To manage them, use “AddRaycast” or “RemoveRaycast” from a script. Persistent Raycasts must be created from a screen point.
When you create a new ARRaycast, ARFoundation generates a new GameObject with an ARRaycast component. You can also provide a prefab in the “Raycast Prefab” field, which will be instantiated for each ARRaycast, allowing you to customize its behavior.
STEP TWO —AR Placement Interactable Component | *AR Placement Interactable GameObject
[Scene > XR > AR Placement Interactable] → AR Placement Interactable → [Adjust AR Placement Interactable]
XR Interaction Manager GameObject is automatically added to the scene as it is needed for communication between Interactors (AR Gesture) and Interactables (AR Placement).
AR Placement Interactable Component controls the placement of Prefabs via a tap gesture.
— Reference XROrigin GameObject → Allows component to vital information from the devices camera
— Reference Cube Prefab for Placement Prefab → When a raycast interacts with a plane, this prefab is what will be instantiated.
STEP THREE — AR Gesture Interactor Component | MainCamera
XR ORIGIN (XR RIG) > CameraOffset > MainCamera > Add Component > AR Gesture Interactor
AR Gesture Interactor Component allows the user to manipulate virtual objects through gestures and is the component responsible for letting the program know when the fire a raycast.
— Reference XROrigin GameObject → Allows component to vital information from the devices camera
— Reference XRInteractionManager Component → Specifies which Interactor to communicate with