Getting a Closer Look and Putting the Object Back | AR Unity Developer

Derek Anderson
3 min readMay 4, 2024

--

Previously, the AR app I have been working got a new scene with an examiner function, thanks to some C# scripting. Now that this app can examine an object up close, the next step is to be able to unexamine or deselect an object.

In order to place an examined object back to where it was, the position data of the examinable needs to be cached in the ExamineManager script. This can be done with a Vector3 variable, which I will call _cachedPosition. This will be initialized at its default value. With this, we can cache the position of the examinable to the original transform position.

private Vector3 _cachedPositon;
.
.
.
public void PerformExamine(Examinable examinable)
{
_cachedPosition = examinable.transform.position;

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

With the position now cached, the examinable can be moved back. However, the examinable only exists as a parameter in the scope of the PerformExamine method. Store a reference to it by creating a new variable and setting it within PerformExamine().

    private Vector3 _cachedPosition;
private Examinable _currentExaminedObject;

.
.
.
public void PerformExamine(Examinable examinable)
{
_currentExaminedObject = examinable;

_cachedPosition = examinable.transform.position;

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

At this point, the PerformUnexamine method can be written, which will basically undo what PerformExamine() does. I’ve rewritten portions of PerformExamine() to appear more readable. The last part of PerformUnexamine() will undo the parenting of the examinable object.

    public void PerformExamine(Examinable examinable)
{
_currentExaminedObject = examinable;

_cachedPosition = examinable.transform.position;

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

public void PerformUnexamine()
{
_currentExaminedObject.transform.position = _cachedPosition;
_currentExaminedObject.transform.parent = null;
}

Now PerformUnexamine can be called from the Examinable script within the method RequestUnexamine(). The print portion is just an additional check to see that the method is working properly.

    public void RequestUnexamine()
{
_examinableManager.PerformUnexamine();
print("Unexamine has been requested");
}

RequestUnexamine () can now be hooked up to the examinable in the Unity Editor. In testing this out, it mostly works, but the rotation of the examinable object isn’t remembered.

Before moving on to the rotation of the examinable object, I’m going to install another package to Unity called Android Logcat since I am developing and building to an Android phone. Android Logcat is a tool to use in debugging, reading console logs, and running on an actual device. Once installed, this package can be found in Window -> Analysis -> Android Logcat. If your device is plugged in and trusted by the computer, you’ll be greeted by a vast number of logs for your Android device. This can be filtered to only look at the Unity AR app logs that we’re working on, and this tool is similar to looking at console logs on Xcode on a Mac device.

Caching the rotation is similar to how the position was cached. Instead of using a Vector3 variable, this variable will be a Quaternion, which is used for storing rotations.

private Vector3 _cachedPosition;
private Quaternion _cachedRotation;

.
.
.
public void PerformExamine(Examinable examinable)
{
_currentExaminedObject = examinable;

_cachedPosition = examinable.transform.position;
_cachedRotation = examinable.transform.rotation;

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

public void PerformUnexamine()
{
_currentExaminedObject.transform.position = _cachedPosition;
_currentExaminedObject.transform.rotation= _cachedRotation;
_currentExaminedObject.transform.parent = null;
}

Our AR app can now examine a selected object and unexamine that same object to its original position and rotation.

--

--