Placing an Object on a Plane in AR
Tutorial
·
Beginner
·
+10XP
·
35 mins
·
(313)
Unity Technologies

In this tutorial, you will learn how to utilize plane detection in AR foundation and tap gestures to place an object on a horizontal surface.
Languages available:
1. Overview
This tutorial will help you understand how to use plane detection in AR foundation to place an object on a plane. In addition, you will learn how to instantiate an object using a tap gesture. You will create an AR app that has the functionality to place an object on a detected plane with a tap.
2. Enable Plane Detection
Learn how to enable plane detection with AR foundation
1. Go to the Hierarchy and double click on the AR Session Origin object
2. Add the plane manager component (Figure 1)

Figure 1: Drop down menu to add the component
3. Select Horizontal in the drop-down menu for Detection Mode (Figure 2). You might have to click Nothing first, before setting it to Horizontal.

Figure 2: Drop-down menu for plane detection modes
4. The manager is now tracking horizontal planes.
3. Visualize Planes
After enabling plane detection, learn how to visualize the planes with a Prefab object.
1. Create an AR Default Plane object in the Scene view by selecting GameObject > XR > AR Default Plane from the context menu that appears (Figure 3)

Figure 3: Create an AR Default Plane Object
2. Save the AR Default Plane as a Prefab
3. Drag and drop the AR Default Plane Prefab into the AR Plane Manager Plane Prefab field (Figure 4)

Figure 4: Drag and drop the asset into the field
4. Delete the AR Default Plane GameObject in the Scene
5. Result: plane detection for horizontal planes (Figure 5)

Figure 5: Detected planes
4. Place cube on plane
Use AR Raycast Manager and AR Reference Point Manager to place a cube on a plane when a user taps the screen. The use of reference points improves tracking accuracy of the application and is normally used as a best practice when creating AR content.
1. Add an AR Raycast Manager to your AR Session Origin
2. Add an AR Reference Point Manager to your AR Session Origin
3. Create a new C# Script and call it TapToPlace
4. Add the TapToPlace C# script to your AR session origin
a. This script uses raycasting and touch detection to place the cube on the plane depending on where the user tapped on the phone screen.
4. Create an empty GameObject (Figure 6)

Figure 6: Create an empty GameObject
5. Create a cube. We want to alter the scale and position to make it sit realistically on the plane (Figure 7)

Figure 7: Cube with the right dimensions and positions
6. Set the scale of the cube to 0.5 in all dimensions in the Inspector
7. Set the position of the cube to be 0.5 in the y dimension
8. Drag the cube under the empty GameObject to parent it to the empty GameObject (Figure 8)

Figure 8: Cube is a child of the empty GameObject
9. Create a cube Prefab and drag the parent GameObject into the field for the script component (Figure 9)

Figure 9: Script with Prefab in the Inspector
11. Build and run your project. Make sure to tap your screen to instantiate a cube!
12. Result (Figure 10):

Figure 10: Placed cube on the plane
5. Conclusion
In this tutorial, the user has added plane detection, reference points, and touch gesture functionality to their AR Foundation project. This project serves as a stepping stone for the user to customize the project for different use cases.