So far we've programmed in the ability for our instant check to be turned on.
But we haven't figured out how to turn it off and we're not even sure from our interface if it is in
fact on except by the fact that it does track and move when you move the camera.
OK.
So what we're going to do now is actually update the state change back into your code.
As I said in the previous lecture what happens when you press the button is that it goes ahead and is
Shiite's this Cheka sit state and turns the tracker on.
But we have no way at this point of knowing if the Cheka has been successful and no way are we setting
this is changing back to false so that we can press the button again and come in and turn it off.
So that means we need a callback method in here that the tracker itself can call.
So let's just go underneath that method there and we'll add in a new one for state change.
All right.
And it looks like this now is going to be public because it has to be called from outside of the script
on state changed.
You pass an instant checking state new state so the whatever state the checker is now in whether it's
on or off is going to come in to this method.
Now if the track of state is initialising then we're going to set our messages on the button.
We will set it to stop tracking.
So this means at this point if it's in this initialization that means it's not turned on it's just sort
of ready to go.
So the button should display stop tracking so that you can click on it to start tracking the message
bookstand the bottom is going to say not tracking which is going to be obvious from the fact that the
grid is not working a that.
Now if we're turning it on then it will change the button to stop tracking.
And in the message box is going to say tracking.
Now this is all very nice.
This is all the interface type thing.
The important part about this code is changing is changing to false which will allow the toggle button
to toggle again so that it can then turn the thing on and off all of the time.
Now this onstage changed gets called from the tracks so save your code.
We're going back into unity.
We're going over to the instant track to click on that.
Now if you have a look in the inspect it you'll see a number of different events that you can take advantage
of writing your own code and you can see that at the very bottom we've got an on state changed.
So here is where we're going to assign our control as the object.
And then the function will be that when we just write.
So let's just drop this down go to the controller and you'll see on state changed at the top.
So now when the tracker turns from off to on it will run this and when it turns from on to off it will
also run this.
Let's have a look at the difference.
So press play and then we will just move the camera around like that.
Now you can say that the button is saying stop tracking and we're currently not tracking.
So if we click on the button it says stop tracking which means we are tracking and you can see that
by the message down the bottom so I should be able to move my camera and we will see a grid move.
OK so what he's actually performing the slam operations to track the movement of the environment.
And now something we couldn't do before is we can click on stop tracking to turn it off.
And then if we move the camera you can see that the grid is now fixed and no longer tracking the environment.
So that's working very nicely.
We'll now add a little more visual clues to the application.
Let's go back into control.
In the update which we haven't yet touched.
We're going to detect the state of the Cheka and then change the color of the green.
So inside of your update add in the code here what it's doing is checking if the track is initialising
So it's not turned on but it's ready to go.
And it can start tracking so it's available to start tracking then it's going to show up as grain.
Otherwise it will be the target color of the default target color.
Now this default target color is the color we saw right at the beginning of the last lecture on that
grid render a script that we've attached to the controls.
So these orange color that's going on here and of course you can change that to whatever you like.
Back in the control.
So we've got green.
Otherwise it's going to be orange.
Now if we've already started tracking We're going to get the orange color on.
I think so we should get green when we first start playing.
So with that in this say it switch back into unity is press play and have a little look and new grid
I guess.
So now it's green which means it's ready to go.
It's not currently tracking.
Now if I might want to turn it down on the flat surface and then stop tracking so that now I'm going
to try on get that plane you can see I'm still not absolutely perfect because using this Web cam and
just moving it up and down is not the best way to do this but the because the whole idea is if you want
something to look like it's go and he walking across this table top then you want this grid to appear
to be sitting over the top of this table top.
Now the way I started tracking there is a little better.
So these two grids are now pretty much parallel with each other.
So all we really need to do is get them aligned.
And once you start building this out to all devices and doing it's going to work a whole lot better.
So just trust me finale's that we are getting to the point of lining agreed with the real world.
So we have our camera and we have our pipeline projected into the virtual environment.
Let's add some content.
So we're going to add in some SPEEs just to start with and to test things out in the hierarchy.
Right click 3D object and create a SPia.
Now this is going to become a prefab and we will then click on how augmented world and add this feature
to the ground plane.
It's going to be quite big.
At one in scale and zero point zero five for h the x y and Zed dimensions works out quite nice for that.
So just create yourself a sphere and you can color it if you like.
Let's drag and drop that into assets and that will make it into a prefab.
Then delete this the that's in the environment.
Now we'll go back into our controller code.
The first thing we need of course is to pass through our prefabs So let's put another public object
at the top and we will call this the object prefab like that if you say that switch back to unity click
on your controller and then drag and drop your speed into that open property so the Albe prefab.
So any input messages that need to be received have to happen inside ovule update.
So come down and find your update then the first thing we want to do.
We're also turning the color on and off for the state of that grid.
But at the top we're going to just add in these.
Right so this code is saying if input get a mouse button down 0 this is the equivalent of a touch on
your device screen or a click of the mouse at the point that you click on the mouse.
We're going to convert that mouse position using the camera's transformation into a Ray that is projected
into the environment.
So a screen point to right will do that for you.
Then we're going to use that to figure out where the intersection between the ground plane and FEMA
right cast is.
But before we do that we need to create the plane that will become the thing we're going to heat.
So to do that we first of all create a unity engine dot plane call at ground plane and it's easy to
create with new unity engine plane and we're positioning it at the zero position in the zero rotation.
And to do that it's vector three dart up and then vector 3 0.
This gives you exactly the same plane that you would get if you go to create a plane in the hierarchy
and you right click and go 3D object plane.
Now Actually that's not entirely true.
It gives you the same sort of geometry as that but it's not going to have a collider or anything on
it and you won't be able to see it either.
So doesn't have a renderer it's not a game object.
It's it's a plane just a straight plane construct.
Now we also need a flight called Touch position that's going to record how long the Al right went into
the environment before it touched the plane.
So to do a touch on apply or recast heat on a plane the plane itself has that ability so it's the ground
plane.
Right.
Cost using our right from the camera and then finding out how far along that right it hit that plane
that's going to give us a nice position in L world.
If we then use that length that we've calculated put that back into the RE and find the actual point
along it's length at the touch position and grab hold of that which is our three day coordinate of the
hit of the right and the plane.
And what's so great about that is that we can use it for real 3D objects positioned so that we're creating
a new game object instantiating it from an object pre-fab at the position.
We've calculated that we touched and just using a zeroed rotation for us.
All right.
So let's say this and have a go.
OK so press pressed play and I've got the camera initially pointing straight down the Grand Am going
to press the start tracking button.
At this point you'll see that I get a speed.
Another reason is the update is running regardless of what the camera is doing and whether it's tracking
or not at the moment.
So any touch on the screen is going to give us a hit on the plane that we've created.
OK so now I can move this up that on tracking and notice how it's tracking that bullet's keeping that
ball attached to that ground plane.
It loses the ability to track the point cloud at some points.
But let's go here.
We'll add in a few more and you can see it getting smaller as well into the distance so your perspective
is being maintained.
And with clicking on that plane and of course now because we're tracking we can move and also track
the environment which is going to make sure that plane is moving relative to all of the feature points
that it's found in that Kemmerer.
All right.
So this is the start of being able to put things onto our plane so let's just stop that.
OK so next what we want to do is stop Boll's baying produced all the time we only want them when they
were tracking.
So if you go over to your instant tracker and then find the trackable underneath it it's the trackable
that knows where the tracking is happening and it's going to happen in the on scene recognized and on
scene last.
So that whenever it finds a sane creates a point cloud that it can follow from this.
And then when it loses that it will run.
So we could set a value for tracking in here to true and then set it for false in there so we can write
this code back in the controller.
Let's go back into the CTRL-A.
First of all we need a boolean to store a value so we're going to go up to the top and add in that.
Let's go bhool is tracking and we'll initially set it to False.
Then somewhere in your code you need to add in two more methods or just put them up the top make it
easier.
And I looked at that.
So one is on scene recognized taking in the instant target value which we're actually not using in.
But this is the method format that is required for that.
And in sort of it is tracking becomes true.
And then on scene last is tracking becomes false.
Now we can use is tracking if we go down into our update as a condition on adding something.
So let's go into here and go.
And is tracking like that.
So just say that go back into unity and we'll click on our trackable for the on scene recognize when
I click on that drag and drop your controller into that box.
And then we find the function that we just created in the controller which is on Sayne recognized the
same thing for the last put in the controller guy find on StN last like that.
Now when we press play we won't be able to create the balls onto the surface.
Unless we've turned the tracking on like that now we still get one.
We go to turn the button off which we can avoid with a little bit more work.
So let's head back into the code.
What we want to do on this mouse click is to check whether we're over a you are at home or where over
the scene.
And if we're over the scene then we can put a ball down.
But if we're over a button or some other item it'll stop the click from going through.
So any We put in and and we want to say not a vent system.
Current dot is pointer over game object which is a method.
Now this method will then tell us if we're over a item or R-Va. the game when we click.
Now in order to use this event system we have to go up to the top and add that library in so it let's
go using Junichi engine dot event systems like that.
OK so we save it.
Switch back to unity and we will try it at all right so just get back to normal scale.
OK so we can click and put anything down stop tracking and we can put things down.
We put things down under the button.
You'll notice but we can turn the tracking on and off which works the way we'd like it to.
And now that that's there and actually tracking those balls will stay put.
As we move the camera around now since we're already creating these spheres we might as well use those
as the bowling balls.
We want to roll across our augmented saying.
The only problem at the moment besides the fact that the balls just stick to one place is that there's
no physics to make them roll and there's nothing in the scene with a collider on it that will allow
them just to roll across it.
So in the next video we'll come back and we'll start to add some virtually solid objects I guess as
far as a grand plan which will be just a shadow plane which will sit on top of your actual table top
and give those fees some shadow and the ability to use a physics system in unity to make them roll across
the surface away from where you click and into the distance.