Getting started with AR object placement in Unity

In our previous tutorial we saw how to detect planes and estimate the ambient light using AR foundation. In this tutorial we will see how to place an object on the detected plane at the point of touch and move the spawned object around the scene.

If you are totally new to AR it’s better to go through out previous tutorial on How to get started with Unity AR and Light estimation in Unity AR before proceeding with this.

Setting things for AR raycast

To place an object in the scene we are going to use Unity AR raycast Manager to cast a ray from the touch position. If the ray hits a place, we will instantiate a cube in that position. If the cube is already present in the scene, then we will move it to the touch position.

AR raycast is similar to regular Raycasting in Unity. We get a list of objects hit when we cast a ray.

Before using raycast in AR you need to add the AR raycast Manager to AR session origin.

Select AR session Origin and go to the inspector window and click on add component. Select AR raycast manager from the list.

Don’t add any prefab to AR raycast Manager. We will be using a separate script to spawn the object.

Script to spawn object in AR

Here is the script to spawn the object when the screen is touched. You can just attach this script to AR session origin and drag and drop your prefab.

Unity sale

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

public class Spawn_object_AR : MonoBehaviour
{
    public GameObject spawn_object;
    GameObject spawned_object;
    ARRaycastManager Arraymanager;
    bool object_spawned;
    List<ARRaycastHit> hits=new List<ARRaycastHit>();
    
    void Start()
    {
        Arraymanager=GetComponent<ARRaycastManager>(); 
        object_spawned=false;       
    }

    
    void Update()
    {
        if(Input.touchCount>0)
        {
            if(Arraymanager.Raycast(Input.GetTouch(0).position,hits,TrackableType.PlaneWithinPolygon))
            {
                var hitPose=hits[0].pose;
                if(!object_spawned)
                {
                    spawn_object= Instantiate(spawn_object,hitPose.position,hitPose.rotation) as GameObject;
                    object_spawned=true;
                }
                else
                {
                    spawn_object.transform.position=hitPose.position;
                }
            }
            
        }
        
    }
}

Let’s start with the namespaces.

using System.Collections.Generic; is required to use lists.

using UnityEngine.XR.ARFoundation; is required for AR raycast manager.

using UnityEngine.XR.ARSubsystems; is required to know the trackable type.

Create a public gameobject to get the prefab to be instantiated.

Next create a gameobject to reference the instantiated object in script.

Declare a boolean and reference to AR raycast Manager.

In the start function get the AR raycast Manager component and set the instantiated Boolean to false.

In the Update function check if the screen is being touched. If yes cast a ray from touch point. If the cast ray hits a plane, then check if the object has already been spawned if not then instantiate the prefab.

If the object is already present in the scene, then change its transform to hit position.

You have learnt to place objects on detected planes in AR. If you have any other questions leave them in the comment box below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.