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.

Object placement script for AR Foundation

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.

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

public class Raycast_script : MonoBehaviour
{
    public GameObject spawn_prefab;
    GameObject spawned_object;
    bool object_spawned;
    ARRaycastManager arrayman;
    List <ARRaycastHit> hits=new List<ARRaycastHit>();
    // Start is called before the first frame update
    void Start()
    {
        object_spawned=false;
        arrayman=GetComponent<ARRaycastManager>();
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.touchCount>0)
        {
            if(arrayman.Raycast(Input.GetTouch(0).position,hits,TrackableType.PlaneWithinPolygon))
            {
                var hitpose=hits[0].pose;
                if(!object_spawned)
                {
                    spawned_object=Instantiate(spawn_prefab,hitpose.position,hitpose.rotation);
                    object_spawned=true;
                }
                else
                {
                    spawned_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 for 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.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading