Premium
Placing and Manipulating Objects in AR
Tutorial
Intermediate
30 Mins
Tutorial
Placing and Manipulating Objects in AR
1.
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.

2.
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)

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

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

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

6.
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)

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

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

9.
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;

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

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

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

13.
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); } }

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

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

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

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

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

19.
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)

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