Placing and Manipulating Objects in AR
30 Mins
Placing and Manipulating Objects in AR
Create AR Session GameObject
From the GameObject drop-down, select XR > AR Session. You won’t be interacting with the AR Session directly, but rather allows the AR world to interact with the real world.

AR Camera Orientation
Next, add an AR Session Origin. This provides the AR camera and orients the AR world in the real world. Delete the existing Main Camera in the Scene and assign the MainCamera tag to the AR Camera. (Figure 01)

Create Placement Indicator GameObject
When you place an object in AR, a placement indicator will tell you if you’ve selected a valid location.
To create a placement indicator, create an empty GameObject and name it Placement Indicator.

Use a Quad as the Placement Indicator
From the GameObject drop-down in the Unity Editor, select 3D Object > Quad.
Drag and drop the Quad onto Placement Indicator, making it a child of Placement Indicator.

Reset Object Transforms
In the Quad’s Inspector, zero out the position and rotate 90 degrees about the X axis. (Figure 02)

Identify the Placement Indicator with a Texture
Rather than a solid square, you may want to put a reticle texture on the Quad. In that case, set its material type to Unlit/Transparent and drop a reticle image into the Texture slot. (Figure 03)

Create a new GameObject to hold a script
Create an empty GameObject and name it Interaction.

Attach a new script to the GameObject
Attach a new C# script to Interaction, named ARTapToPlaceObject.

Create the Code
Double click ARTapToPlaceObject in the Project View to open it in your Script Editor.
Beginning on line 4, type:
usingUnityEngine.XR.ARFoundation; usingUnityEngine.Experimental.XR; usingSystem;

Define Public Variables
Inside the class definition, before Start(), type:
publicGameObjectobjectToPlace; publicGameObjectplacementIndicator; publicCameraARCamera; privateARSessionOriginarOrigin; privatePoseplacementPose; privateboolplacementPoseIsValid = false; privateboolobjectPlaced = false;

Create Start
Inside Start(), type:
arOrigin = FindObjectOfType<ARSessionOrigin>();

Inside Update(), type:
UpdatePlacementPose(); UpdatePlacementIndicator(); if(placementPoseIsValid && Input.touchCount > 0&& Input.GetTouch(0).phase == TouchPhase.Began && objectPlaced == false) { PlaceObject(); objectPlaced = true; }

Update functions
In Update(), we call a few functions that haven’t been defined yet. Let’s do that now. Create some whitespace between the Update()method and the end of the class, and type:
privatevoidPlaceObject() { Instantiate(objectToPlace, placementPose.position, placementPose.rotation); } privatevoidUpdatePlacementIndicator() { if(placementPoseIsValid && objectPlaced == false) { placementIndicator.SetActive(true); placementIndicator.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation); } else { placementIndicator.SetActive(false); } } privatevoidUpdatePlacementPose() { varscreenCenter = ARCamera.ViewportToScreenPoint(newVector3(0.5f, 0.5f)); varhits = newList<ARRaycastHit>(); arOrigin.Raycast(screenCenter, hits, TrackableType.Planes); placementPoseIsValid = hits.Count > 0; if(placementPoseIsValid) { placementPose = hits[0].pose; varcameraForward = Camera.current.transform.forward; varcameraBearing = newVector3(cameraForward.x, 0, cameraForward.z).normalized; placementPose.rotation = Quaternion.LookRotation(cameraBearing); } }

Save Script
The script is now complete. Save changes and return to the Unity Editor.

Create an instance of the GameObject
To instantiate your Prefab, drag it into the slot marked Object To Place.

Set Placement Indicator
Drag your Placement Indicator into the slot marked Placement Indicator.

Set AR Camera
Drag the AR Camera into the slot marked AR Camera.
The Interaction setup is complete. (Figure 04)

Setup Raycasting
We’re almost ready to build and test our project. We still need an EventSystem for our Raycast to work. From the GameObject drop-down, select UI > Event System.

Build and Deploy
Build and deploy your project. You should now be able to place objects in valid locations in AR, as indicated by the Placement Indicator. (Figure 05)

Getting started with AR in Unity is easy, and the possibilities are endless.