In continuation to our AR tutorial series today we are going to see how to use Anchors to keep your objects in place in the real world. If you are totally new to AR, check out our other tutorial on
What are AR anchors?
AR anchors are components of Unity AR foundation that lets you fix the position of an object relative to other feature points in the real world.
For example, if you want a cloud at a height of 1 meter from the surface you can place an AR anchor there and spawn the cloud at that point. Now even if you move around the cloud will remain at that position.
An important point to note is AR anchors are performance expensive. A lot of computation goes into keeping the anchors in place. You must try to limit the number of Anchors you use to reduce the computational load.
How to place AR anchors?
You can either add an AR anchor to an existing gameobject or spawn a prefab with AR anchor component.
Before adding the AR anchor component, you need to add the AR Achor Manager to the AR Session Origin.
AR anchors won’t work without features points like planes. So, you need to detect at least one plane for the AR Anchors to work.
To add AR anchors to the prefab, select the prefab and click on Add component. Search for ARAnchor and click add.
To add AR anchors during runtime using code., use the code below.
Things to know about AR anchors
- AR anchors are performance expensive. Your FPS will drop if more Anchors are present in the scene.
- AR anchors won’t work without feature points like plane. You can disable plane detection after a few planes have been found if you don’t need them.
- You should not change the transform of the AR anchor. Unity foundation takes care of it. If you need to change the transform of an object with AR anchor then destroy the Anchor component, move the object and add the AR anchor back.