Creating an AR Object Examiner in Unity

Paul Killman
Antaeus AR
Published in
4 min readMar 15, 2024

In previous articles I described how to place AR objects into the real world on mobile devices. I also described how to scale, rotate, and translate them. Finally, I described how to create a simple menu system that lets you place multiple types of objects into the real world. None of the abilities above relied on any type of scripting.

In this article, I will describe how to select and examine an AR object using scripting.

I began by creating a new scene in my project. I named it Object Examiner.

Because the initial setup was so complex, I have described it in a separate article.

The way that I see this working is to have two scripts — one on the object to be examined and another to manage the examination.

The Placement Prefab Cube has several scripts attached to it that allows us to select, translate, scale and rotate it. This seems to be the logical place to add a script that will allow us to examine it.

I created a new script called ObjectExaminer and added it to the prefab.

I created an empty object in the scene and named it Object Examiner Manager. I created a new script called ObjectExaminerManager and attached it to the new object.

Underneath the Main Camera, I created an empty object to act as a target for the object that we are examining. I named it Examine Target and made sure that the Transform values were all zeroed out.

Here is the way this process will work. When an object is selected, it will be made a child of Examine Target. This will ensure that it is in the screen, no matter what direction the camera is facing. When we are done examining the object, it will be returned to its original position.

Here are the two scripts that I created earlier to accomplish this.

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class ObjectExaminerManager : MonoBehaviour
{
[SerializeField]
private Transform _examineTarget;

private Vector3 _cachedPosition;
private Quaternion _cachedRotation;
private ObjectExaminer _currentExaminedObject;

/*
* Cache original transform info and then
* move object to Examine Target position
*/
public void PerformExamine(ObjectExaminer objectExaminer)
{
_currentExaminedObject = objectExaminer;
_cachedPosition = objectExaminer.transform.position;
_cachedRotation = objectExaminer.transform .rotation;

_currentExaminedObject.transform.position = _examineTarget.position;
_currentExaminedObject.transform.parent = _examineTarget;
}

/*
* Return object to its original position and rotation
*/
public void PerformUnexamine()
{
_currentExaminedObject.transform.position = _cachedPosition;
_currentExaminedObject.transform.rotation = _cachedRotation;
_currentExaminedObject.transform.parent = null;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
* This is called by the
* AR Selection Interactable
* Select event
*/
public class ObjectExaminer : MonoBehaviour
{
private ObjectExaminerManager _objectExaminerManager;

void Start()
{
_objectExaminerManager = FindObjectOfType<ObjectExaminerManager>();

if(_objectExaminerManager == null )
{
Debug.LogError("Object Examiner Manager Not Found!");
}
}

/*
* Request for the object to be
* examined when selected - Select event from
* AR Selection Interactable on object
*/
public void RequestExamine()
{
_objectExaminerManager.PerformExamine(this);
}

/*
* Request for the object to be
* unexamined when unselected - Select Exited event from
* AR Selection Interactable on object
*/
public void RequestUnexamine()
{
_objectExaminerManager.PerformUnexamine();
}
}

To make this work, I added the ObjectExaminer script to the Select and Select Exited events in the AR Selection Interactable on Placement Prefab Cube.

Now, when the prefab is selected, it calls the RequestExamine method in the ObjectExaminer script. When the prefab is unselected, it calls the RequestUnexamine method.

I tried it out and could not see the examined object. I realized that it was being placed in the same position as the Main Camera because the target was zeroed out. I played with the position of the target some until I was happy with the placement.

Here is the result.

Place Cube
Examine Cube
Unexamine Cube

It works!

--

--