One of the features of AR Foundation in Unity is Image Tracking. It’s one of the “oldest” methods to position virtual elements in the real world.
Is this still relevant today? Of course, it is! Markers are still helpful for the users because you can place them exactly where you want them to be. You can’t do this marker-less yet.
So, do you want to learn how to set up the project to use image tracking for AR?
Setup project ??
As with other posts about AR (Face Tracking and Building AR Game), we have to start with the Package Manager and its packages for AR.
You will have to install the following packages: AR Foundation, ARCore XR Plugin (for Android support), and ARKit XR Plugin (for iOS support).
Now we can start working. ?
Creating Marker
The first thing we should do now is to set up the markers for our project.
To do that, go to the Project window and click Create > XR > Reference Image Library.
Now you have to import an image which you want to use as a marker. Add it to the newly created ReferenceImageLibrary object.
I would also recommend specifying the real-life size of the marker. If you have a print of it, just measure it on the paper.
Build AR scene ??
With every AR scene, we have to start by adding AR Session and AR Session Origin to the scene.
You can do it by going to the Hierarchy window and clicking Create > XR > AR Session and Create > XR > AR Session Origin.
Image Tracking ?
When you have both of them, you can add AR Tracked Image Manager to the AR Session Origin game object.
Of course, now we also have to assign the references in it.
One reference is for our ReferenceImageLibrary and the second one if for our prefab which will be displayed on the marker. Be aware that 1 unit in Unity space is 1 meter in the real world!
You can also specify how many images you want to track. The maximum number may vary between different devices so I would recommend checking it (read more).
Build settings ?
Depending on the platform you are using, you may want to change some build settings.
For Android, if your project requires AR support, you have to use at least API Level 24.
And for iOS, you will have to use at least iOS 11.0 and check “Require ARKit support“.
The result ?
And as you make everything correctly, now we can launch our AR app!
Great! It’s working! At least for me… ?
If you don’t see the object there, your scale may be too big for AR, so try to walk away from the marker. ?♂️
If you find this useful, let me know in the comment section below or share it with your friend! I would really appreciate that! ❤️
And if you are interested in getting emails when I release new post sign up for the newsletter!
You can also check the whole project at my public repository. ?
See you next time! ?
hi. thanks for the tutorial. is it possible to keep tracking the image after object is spawned?
The image is still tracked after an object was spawned. But you can still listen to the trackable events.
Doc:
1. https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@2.1/api/UnityEngine.XR.ARFoundation.ARTrackedImage.html
2. https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@2.1/api/UnityEngine.XR.ARFoundation.ARTrackable-2.html#UnityEngine_XR_ARFoundation_ARTrackable_2_trackingState
Hello,
thanks for the great tut.
Is it possible to add different objects and different images in the same project?
The flowe image shows a 3D flower, the dog image shows a 3D dog and so on.
Thanks in advance.
Sure! You just need to add new AR Tracker Image Manager component to the game object and assign different Image Library. With that, you can assign different prefabs for different images. ?
it seems not to work, can you help a litle
this dose not work for me
Doesn’t work, this component is a singleton, you can only have one per scene. If you know how to do it let us know please
Thanks for the tutorial!
If someone is looking for a way to test Image Tracking in Unity Editor, I made a plugin that supports this.
https://forum.unity.com/threads/ar-foundation-editor-remote-test-and-debug-your-ar-project-in-the-editor.898433/page-2#post-5986280
Thank you for the tutorial 🙂
I followed the tutorial, but both on iOS and Android, the camera feed isn’t showing. Any idea of why that may be?
I’m using Unity 2019.4.1f
Where is “AR Tracked Image Manager”? It doesn’t seem to exist in my project or links.
Did you change a project platform to Android or iOS? And do you have AR Foundation added though Package Manager?
The project platform is set to Android (it was by default set to “PC, Mac & Linux Standalone), and yes AR Foundation, ARCore XR Plugin, and ARKit XR Plugin are all installed.
Yeah, thanks. Somehow the Android SDK didn’t come along when I did a Unity update. Had to delete Unity, UnityHub, registry entries, …. royal pain in the whatname. got it now.
Actually, I thought it was solved, but no. So, where do I find “AR Tracked Image Manager”?
if you can help me to set up scean
I have 10 prefabs I want to be triggered from 10 different images.
Hi all, Thanks for the tutorial. I managed to create an application based on ARFoundation and Image tracking and it works well. I could notice that the camera pose is computed/given in the AR session coordinate system (the origin being the position of the camera when the AR session started). But I’d like to be able to get the camera pose with respect to the tracked image i.e. expressed in a coordinate system based on the image. For that, I need to get the image position expressed in the AR session coordinate system but I could see it is given… Read more »
Is it possible to add a Collision Detection to the Game Object on the tracked image? I want to create a sword fight simulation. For example: On a card I track a sword and if I hit another object, which was not tracked on an image, this object should turn to the color red.
How Canada you do it?
Yeah, sure. You can do whatever with them 🙂
They are like any other game objects in the Unity scene.
Impossible de build.
UnityEditor.BuildPlayerWindow+BuildMethodException: Error building Player because scripts have compile errors in the editor
Malgré avoir suivit scrupuleusement le tuto :/ …
Have you switched build to the Android or iOS platform? What’s the error?
Hi. Thanks for the tutorial. But my image(imported as .obj and converted to Prefab) doesnot get fixed to the tracked image. Even if I move, away my prefab comes along with the camera. But is works for inbuilt object like Cube, Sphere. Can you please help out.
Hi, thanks for the tutorial.
I have a problem. I’m trying to connect my Samsung S21 using Unity Remote 5 app, to test my project. But I saw in my mobile screen the background and not the camera view. So I cannot Image Track the marker.
I check the Android SDK, Google USB Driver, Android devices in the project.
I dont know what else to do. Have you any idea what to check? Thank you.
I’m not sure if you can test it through Unity Remote as all AR solutions have to run on the phone. With the remote phone works just as a display.
hello,the object when I move away from the marker moves, do you know why this can happen?