Placeholder Image

Subtitles section Play video

  • What is up everyone and welcome to a new tutorial about Spatial Anchor.

  • So, we previously learned how to create mixed reality applications using the Unity XR Toolkit.

  • We did some plane detection to set plane on the position of our walls, ceiling, and floor in the real world.

  • But how can we actually anchor not just plane but any object anywhere to our real world?

  • And this is what we are going to see with the Spatial Anchor.

  • As the name suggests, the Spatial Anchor will use the feature of the real world to set the position of an object in your game.

  • This means that the object will not follow your game, which is pretty cool if you don't want the object to move when the player is centered or leave the headset.

  • Now, I hope you guys are ready for the first tutorial of 2024 and if you are, make sure to leave a like down below.

  • You can as always support my work on my Patreon where you will be able to find the source code of all of my tutorials and some exclusive content.

  • But without further ado, let's get started!

  • Okay, so we are back where we were left at the end of the second episode about using the Unity XR Toolkit to make a meta mixed reality application and this is the project that I'm going to use to show you how to use the Spatial Anchor.

  • So, first thing first, as you can see, there is a big change from the last episode.

  • It is that now the version of Unity that I'm using is 2023.2 and not 2022.

  • Now, this is important because if we go to Windows, Package Manager, there if you go to In Project, as you can see, the version of AR Foundation that we are currently using is the version 5.1.1, but this version is actually not the latest one.

  • There is actually a new version, so the 6.0, which has changed some of the stuff from the AR Foundation that I want to use because I want to make this tutorial last as long as possible.

  • So, to make sure that future people will be able to look at this tutorial right now and do the same stuff as me, well, what we are going to do is update this AR Foundation and, as mentioned, the first step is to use the Unity version 2023 and the second version is to download the Unity AR Foundation 6.0.

  • Now, to download this version of the AR Foundation, if we go to AR Package Manager, AR Foundation, and that we have a look at the version history, as you can see, we cannot see any other version right there and it cannot be downloaded here from the Package Manager, but what we can do is actually download the new version of the AR Foundation by changing the Package Manifest.

  • So, let me close this, then right-click on Asset here and click on Show in Explorer.

  • Now, it should open here an Explorer page and if we go here to Packages, we can double-click on Manifest right there and here you can see all of the packages that are inside this project and if we go here to the XR AR Foundation, we can see the version that we are using is 5.1.1 and, in my case, I want to use the latest one which is 6.0.0.3.5.

  • There you go.

  • Now, make sure to have the same thing as me written over there and to not make any typo for this work.

  • We can then save the manifest with Ctrl S and then go back to Unity.

  • And there you go, as you can see, it is now updating the package.

  • And if we go to Windows Package Manager, as you can see, we can see that we have successfully updated the AR Foundation to the new 6.0 version.

  • Beautiful!

  • Now, let's close this and let's create our first Spatial Anchor.

  • Okay, so the first step to create a Spatial Anchor is to go to the XR Origin right there and click on Add Component to add a AR Anchor Manager.

  • Beautiful!

  • And, believe it or not, but it is the only thing that we need to manage the anchor in our game.

  • So, what we can do now is create the fourth anchor and for this, I think I'm going to right-click, go to 3D Object and create a very simple 3D cube that I'm going to put in front of the player.

  • So, let's first set it to 0, 0, move it forward a bit upward, scale it, maybe do a little rotation.

  • And now, my goal is to turn this cube into an anchor which will be fixed inside our real environment and not moving with the player.

  • So, let's click on Add Component and create a new Beautiful!

  • Now, as the name suggests, my goal is to set this cube to be an anchor at the start of the game.

  • So, for this, we need to write at the top using UnityEngine.XR.ARFoundation and then, we can simply remove here the Update function because, as you will see, it's very simple to turn this GameObject into an anchor at the start because we only need to do GameObject.AddComponent and add a component of type ARAnchor.

  • And beautiful!

  • Now, that's basically it.

  • Simply by adding this ARAnchor component, we will anchor this 3D cube inside our real world.

  • Oh, and by the way, if you want this cube to stop being an anchor in your world, you can simply call DestroyGameObject.GetComponentARAnchor like this.

  • But, of course, this line is just here to show a bit what you should write if you want to destroy the ARAnchor.

  • We don't actually want to do this in this case.

  • So, let me simply remove it like this.

  • Beautiful!

  • And now, let's save and go back to the Unity.

  • Now, what I'm going to do is simply place this cube on the side and duplicate it.

  • And on this new cube on the right, what I'm going to do is remove here the SetAnchorAtStart.

  • Now, my goal is simply to showcase to you the difference between one cube which is anchor and another which is not.

  • So, one thing left to do.

  • Now, it is to build our game.

  • So, let's go to File and click on Build and Run.

  • Okay, so here you go.

  • As you can see, I'm inside the Mixed Reality application.

  • On the right side, I have a cube which is anchor but not on the left side.

  • And let me just show you the difference.

  • If, for example, I turn my head slightly to the left here and that I try to press on the recenter button here on my Oculus right hand controller.

  • As you can see, this cube does not move because it is anchor inside our real environment and not based on the player's position like the rest of the objects are.

  • But, of course, here we are able to anchor some objects at the start of the game.

  • Now, let me show you how to do this at runtime.

  • So, let's go back to Unity.

  • Okay, so now let's see how we can create anchor during runtime.

  • So, to do this, I'm going to create a very simple interaction here which is to add some ray which will come out of my hands.

  • And if I press on the grip button, I want to set an anchor where my ray is pointing.

  • So, first thing first, we need to add a ray interactor.

  • So, for this, let's go under our camera offset here which is located under the XR for ray interactor action based.

  • Beautiful.

  • I'm going to call it right ray interactor.

  • We can go under the XR controller, select here this little icon and search for the right controller preset.

  • Beautiful.

  • Now, we can duplicate this, rename it left ray interactor and do the same but this time for the left controller preset.

  • Beautiful.

  • Now, with this, as you can see, this will add a ray interactor that will follow our left or right hand.

  • And one thing that I want to do is if we select both the right and left ray interactor by pressing on the control key, I want here to set the line bend ratio to one because I don't want the line to bend at all.

  • Beautiful.

  • Now, if you remember in the previous episode, what was actually happening was that there was a near default plane which was for each one of the wall or the ceiling of the floor that we have in our real world.

  • And if we click on this AR default plane, as you can see, it is basically a mesh collider.

  • So, this allowed us, if you remember, to create some collision within the scene from our real world to our virtual one which is awesome.

  • But anyway, I want to use this AR plane as well because I want to make it from the ray interactor.

  • So, let's click on add component and add a xr simple interactable.

  • Beautiful.

  • And that's basically it.

  • With it, we should be able to interact with this plane.

  • So, what's left for us to do is now go back to the right and left ray interactor.

  • I'm going to select them both and I'm going here to create a new component which will be called phone anchor from ray select.

  • Beautiful.

  • Okay.

  • So, again, as the name suggests, the goal of this script is to spawn an anchor from the ray selection.

  • So, for this, we are going to need a public reference to the xr ray interactor which we can call if we go at the top and do using unityengine.xr.interaction.toolkit.

  • And while we are at it, let's add also the unityengine.xr.ar package that we need.

  • And then the two variables that I'm going to need is a xr ray interactor which I can call ray interactor and a public AR anchor manager called anchor manager.

  • Beautiful.

  • Now, what I'm going to do is create a public void function which will be called spawn anchor and which will take as a parameter a base interaction event args which I can call args.

  • So, the goal of using this variable here is that now in the start function, we can do ray interactor.selectentered.addlistener and use here our spawn anchor function.

  • So, basically, what we've done is to hook this spawn anchor function to be called whenever the ray interactor selects something.

  • So, basically, what we can do is then get the hit point and then create an anchor based on this hit point from the ray.

  • So, first things first, let's get the 3D raycast hit with ray interactor.try get current 3D raycast hit.

  • And what we want is write out raycast hit hit.

  • So, here the out raycast hit hit will basically be our output.

  • So, this will be the raycast hit that is currently performed by our ray interactor.

  • And from this hit position, we can actually create a new pose by doing pose hit pose equals new pose and write here hit dot point for the position.

  • And basically, for the rotation, I want to take into consideration the normal of the phase that we hit with our ray.

  • So, let's do quaternion dot look rotation minus hit dot normal.

  • So, hit dot normal is, as I told you, the normal of the surface that we hit.

  • And if we call the quaternion dot look rotation, it will basically create a quaternion, so a rotation, based on this forward axis.

  • So, it will basically align everything to point to this direction.

  • Oh, and here I just write minus because I want the rotation to face to the wall and not out of the wall, but actually as you prefer.

  • This is actually something that I do here because I'm going to need it in a minute.

  • But for now, I'll just copy what I do.

  • So, now that we've created a new pose, what we can do is create an anchor.

  • So, we can do for this var result equals anchor manager dot try add anchor async and give us an input or hit pose.

  • And here is the important function, so the try add anchor async, which is basically adding an anchor at a certain position.

  • But as you can see, this try and add anchor is a nothing function.

  • Async means that we can wait for this function to finish.

  • And this is very important because adding an anchor is not instant, so it takes some time to process.

  • And this can create some issues because if we want to spawn some prefab on the anchor, we need to wait for the anchor to be processed.

  • So, a solution to await is simply to write await here at the front.

  • But as you can see, everything is underlined in red now because we actually need to write at the top async for this to work.

  • And as you can see now, everything seems to work.

  • And finally, with the result, this is not actually the AR anchor that we are getting with this result.

  • So, to get actually this anchor, we need to write bool success equals result dot try get result out var anchor.

  • And here is the anchor that is created if everything went smoothly and that we will be able to use in a minute.

  • But now, with this spawn anchor, if I sum up everything, we first get the hit position.

  • We turn this hit position into a pose, which is basically a point in space.

  • And from this point in space, we create an anchor, which takes some frame to process.

  • So, that's why we write await here to await this to work.

  • And then, once it has done the job to adding an anchor, we have a look at if it succeeds or not and get the output anchor at end.

  • And there we go.

  • Now, that's basically it.

  • Let me save and go back to Unity.

  • Okay.

  • And now that we are back inside Unity, we, of course, need to set up here the spawn anchor from re-select script.

  • So, for the re-interactor, let's simply drag the xr-re-interactor from the same script.

  • And for the anchor manager, we can drag here the xr-origin.

  • Now, let's do the same, but for the left re-interactor.

  • So, drag the re-interactor over there and the xr-origin.

  • Now, everything should run smoothly.

  • So, okay, by adding an anchor from the try-add-anchor async, what this will do is add an empty GameObject with the ar-anchor component to it.

  • But actually, you can override this prefab here with this anchor-prefab parameters.

  • So, if we click here and search for anchor, then click on assets.

  • And if we click on this little toggle there, we should see the anchor transform, which is basically just a GameObject with three axes.

  • I'm going to set this as an anchor.

  • So, this should display when we interact with something.

  • Now, let's save and see if this works by building our game.

  • Okay.

  • So, as you can see, everything still works.

  • I have still this one cube, which is anchor, and this cube, which is not.

  • I have some array, which are coming out of my hands.

  • And if I press on the grip button, as you can see, it works.

  • We can spawn some anchor, which will be placed on our array.

  • And as you can see, this works on all surface.

  • And as you can see, the anchor have the correct orientation.

  • So, everything is perfect.

  • But of course, right now, we are just spawning just little gizmos.

  • What we want is to anchor any content, not just this prefab.

  • So, let me show you how we can do this.

  • Okay.

  • So, if we want to not just anchor the gizmo, but any prefab, what we want is to go back to the script.

  • And at the top, I'm simply going to write a public GameObject called prefab.

  • There you go.

  • And at this point, it is very simple.

  • If we have successfully created an anchor, what we can do is create a spawn prefab by instantiating our prefab.

  • We can actually spawn it at the anchor.pose.position and at the anchor.pose.rotation.

  • Beautiful.

  • And last but not least, we can also set the spawn prefab to have its parent to be the anchor transform.

  • So, this means that if the anchor change position, because the prefab that we are spawning is a child of the anchor, it will move with it.

  • So, that's something really great.

  • Now, let me save, go back to Unity, go to the XR origin, and I'm going to here select this little button and click on None.

  • Because instead of having a gizmo spawn, what I want to do is to spawn a custom prefab.

  • And what prefab should we be able to place inside our world?

  • So, lucky for me, I have this magnificent image that I want to use inside my project.

  • So, what I'm going to do is right-click, go to 3D object, Quad.

  • This will create a 2D quad, as you can see.

  • We can maybe scale it down a little bit and drag here the marvelous image that we have inside this quad.

  • As you can see, this will create also material and apply this image as a texture to the quad.

  • Now, I'm going to call this one Beautiful Painting.

  • I'm going to turn it into a prefab by dragging it inside our project folder and remove the Beautiful Painting by selecting it and pressing the Delete key.

  • Beautiful.

  • Now, what's left is to go to the right and left ray interactor and drag our Beautiful Painting inside the prefab to spawn.

  • This means that now, by doing this, we should have our Beautiful Painting spawning where our ray is interacting with the world.

  • And this means that it will also be anchored in our world and be placed with our real environment and not based on the player.

  • So, one last thing to do and is, of course, to try and find out if this is working.

  • Okay, and here you go.

  • I'm in Mixed Reality right now and let's try to press on the grip button on the wall to see what happens.

  • And as you can see, it works!

  • The marvelous painting is showing with the correct position and at the correct rotation.

  • But the big thing is that all of these are created and anchored to our real world.

  • So, if, for example, I try to recenter the position, as you can see, you can see this cube which is moving because it is following the player's space.

  • But all of the stuff that we spawn and that we anchor stay the same.

  • This is awesome!

  • And basically, this sums up this tutorial on how to anchor GameObject in Mixed Reality to our real world.

  • So, I hope that you enjoyed this first tutorial of 2024.

  • Now, thank you for watching till the end and, as always, if you want to support my work and get access to the source code of all of my tutorials, you can join us on Patreon.

  • The link is in the description.

What is up everyone and welcome to a new tutorial about Spatial Anchor.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it