How to Select an Object in the World | AR Unity Developer

Derek Anderson
3 min readApr 13, 2024

--

Previously, I wrote about tapping cubes to be placed on an AR plane thanks to an AR Placement Interactable and AR Gesture Interactor. It’s now time to explore some more interaction that can be added to this AR app.

There is something that needs to be fixed with the cube placement prefab before moving on, and that is the origin of the cube. Right now, since the cube’s origin is straight through the middle, the cube is intersecting with the plane. The cube needs to be moved up slightly, but the y-axis of the cube needs to stay at zero because the position of the cube will change based on where it spawns.

We can rework the prefab so that the cube is a child of a parent GameObject that will stay at 0, 0, 0. This way it won’t matter where the parent is placed because the child GameObject will be offset from the parent’s position. Drag the cube prefab into the scene, unpack the cube, and create an empty parent to the cube. Name this parent PlacementPrefab.

This parent will have a position of 0, 0, 0 and the child will have the same scale as it did before. Move the cube’s y-axis postion up by half of the scale of the cube, so that the cube looks like it’s resting on the plane. For example, if the scale of the cube is 1, then the offset needs to be 0.5. Once the offset is set, the placement prefab can now be dragged over to the prefab folder. Don’t forget to drag this new prefab to the AR Placement Interactable.

The next interactable to add is the AR Selection Interactable, and this will be added to the placement prefab. This will be added to the parent GameObject and not the cube in order to maintain the offset. Add the component to the parent GameObject. The component has references to the XR Origin and Interaction Manager, but these exist outside of the prefab in the scene, so the references will fill in by themselves. Drag the cube over to the collider section in the AR Selection Interactable component. Colliders are needed because the AR Raycast Manager will use these colliders to act as the hitbox of the cube.

The last field that needs to be filled in for this component is the Selection Visualization, which will appear when the cube is selected. To demonstrate this, I added a cylinder child GameObject to the cube, placed it under the cube, and scaled it accordingly. When a cube gets selected, this cylinder will activate to show that the cube is currently selected. When it isn’t, the cylinder will be inactive. Rename this and drag it over to the Selection Visualization field of the AR Selection Interactable component.

Take some time to create materials for the cube and the selection visualization GameObject. These don’t need to be transparent like the AR default plane, but you should select colors that will differentiate between a selected and unselected cube. Drag these materials to the appropriate GameObjects, and disable the GameObject designated as the Selection Visualization. This should only be seen when selecting the cube and not active by default.

Once that is complete, let’s see it in action!

--

--