From the course: Unity: AR Visualization 03 Complex Interactivity

Accessing the tracked image manager - Unity Tutorial

From the course: Unity: AR Visualization 03 Complex Interactivity

Start my 1-month free trial

Accessing the tracked image manager

- [Instructor] All right. Now it's time to do something with our OnTrackedImageChanged event. What we're going to do is create an alternative way for us to place the jet engine. A way that we can sync between multiple devices. So that sounds very similar to our finding ground state, so let's go and create an alternative method. Let's create a conditional. If state is equal to state dot finding ground, and in here I want to iterate through all of the add OnTracked images, so I'm going to go for int equals zero, i is less than, and we're going to grab that data from pEvent args, pEvent args dot added, a list, so we're going to go dot count all right, and i++, standard stuff. Okay, now we want to make sure that the image that we've detected here and added is the image we want. So the first thing we want to do is go create a constant for the image marker name that we created. Let's go to the top, where constants should live, and we're going to do this probably just under Enums. We're going to call this constants, and it's going to be private. Private const string IMAGE_MARKER_NAME. I'm going to set it equal to what we set it to, which was Marker. Great. So we've got a constant. Scroll back down to our event. Make another conditional, so we're going to say if, our pEventArgs dot added, get the instance, dot referenceImage, dot name. So you're going to make sure you get the reference image. This is the reference image of the data, not the name of the game object itself. If that's equal to the constant we just created, we know that we've got our image. So we know this is what we want. So now what we want to do is exactly what we did when we placed our jet engine. Let's go up to RaycastDetectAndPlace. We want to do these three things here. I'm going to go grab this code. And dump it in here. Great, so we want to set the position of the assets, but we don't want to set it to the raycastHits, we want it to set it to the TrackedImage, so we're going to go and grab that, and now we can go and grab the transform dot position and what position here is fine. What we also want to do is set the rotation of this object. Because now that we've got this marker, we can set exactly the position and the rotation of where we want our jet engine to be. Let's copy this, and change this rotation. And we're going to set that to the transform dot. Rotation. Excellent. The last two things look great. We're going to be setting the state to PickMesh so we can continue and we're going to be saving our fast start position. Now what we also want to do is allow for any updates to the position of our tracked image to be reflected to our jet engine. We only want to do this after we found the ground, so let's go and copy our functionality here. And we're going to go else if PickMesh. Cool, so while we're in the PickMesh state, we're going to do something similar. Instead of looking at the added images, we look at the updated images. Cool. So we got the updated images in our for loop. Need to do the same in the if conditional. So if the updated image is equal to the Marker we're looking at, and then update the position and the rotation accordingly. We don't need to set the state or save the start position. Wonderful! So we've got this great event that is placing a jet engine on our image marker, and we're also updating our jet engine when we move our image marker. That's all we need to do to start syncing our assets between multiple devices. Let's go and build our app and see this in action.

Contents