In this lecture we finally get around to having a look at how the compass works in your mobile device
as you can see on the screen.
He created a really simple compass and I'm using the direction that the phone is facing to sit the rotation
on the compass needle that's on the screen at the top.
You can also see a heading rate at so Becky in unity I'd like you to set up a really simple project
and you can use one of the ones that you've had in the past.
I don't have the wiki jewed Kemmer in this one.
Just a basic camera.
There's nothing special about it.
Just go to a black background on it.
In this scene all I have is a compass needle.
Now this is a model that is attached to this picture as a resource that you can drag in and then drag
into your sane.
Now off go to sitting right in front of my camera.
We just move that a little bit you can see where it is.
The easiest way I find to do this type of thing is if I just delete that compass needle and all I did
and again it's in my folder called directions this folder a he has a few other things that you'll be
using later on in this lecture that are also resources for this.
Now the company angels he is going to drag that and drop it on to the camera.
Whenever you do that it's going to be zero or zero zero.
On to the camera.
And then if we clicked the camera in the sayin you will see it's viewing volume and where that compass
needle is actually sitting.
Now it's right on the camera so you can hardly see.
I mean you can see just a little bit in the game view.
If you get hold of your compass needle and just move along it's Zed axis it will bring it in to your
saying now you need to give it enough Ruru or scale it so that when it turns around on your device the
arrowhead doesn't go off of the edge.
So let me just maybe move it.
And I think I had it scaled down before.
It doesn't matter in this particular example as long as we can see it in the same.
So this is compass you know now you can leave it attached to the camera if you like.
So if the camera then moves then the compass needle is always going to be right in the field of view
and this is another little trick if you want your compass needle in your augmented reality view is make
it a child of the chewed camera so that whenever it's moving around then your needle stays right where
it should.
Which is flash and parallel to the view of the camera.
Right so we've got out a compass needle there.
What we do with it now is we have to give it to the Prius code that we've been working on.
So if I just click on my jeep code this is my controller that we've been using the whole time.
All I've done is add in a compass needle as a public game object so that we can just pass it through
and I'll show you the code in a moment I'll just hook that up there.
OK.
So let's go back and open up that code and have a look.
We already put a few little lines in about the compass very early on when we started creating this code
and out of all of these things that we've done with the GPS system the compass needle has to be probably
the simplest that you can add in.
So first of all at the top what I've got is a public static flight for heading headings going to be
a value that we can use outside of this code just like we did with distance and I'm initially setting
that to zero.
So the heading of your compass is going to return a value between zero and three hundred and sixty or
I think it might be 350 non doesn't actually get to 360 which are all the points on a circle.
Older degrees on a circle.
The next one we have is a public game object for a compass needle which is where we passed in that model
before.
So remember to do that when we save it and run it.
Now let's go down and have a look where all of this is being used.
So the stuff G.P.A. Initially we put some code in there for the compass.
And if we just go down he can see it.
So this input compass enabled true that we've got.
You have to run that in order for the compass to work.
You also need to make sure that you got your location services started which we did for the JPL system
so that's just a little bit further here where we started that.
OK so those lines of code you already have because we put those in there start.
What's new in here is in the update because the updates go indicate setting the heading of the compass
and then applying that to the model of the compass needle to get the heading.
The heading you can see here underneath my message.
I'm going heading equals input compass.
True heading.
Now that's going to get you the true direction for north south east or west from your compass.
The Compass I'm also printing out and you'll see up in the message that I've just replaced direction
with input compass don't you're heading so you can see the values that are associated which with each
direction that you turn and face.
Now to move the compass needle itself the next slide will do that.
The compass needle needs to rotate around its zid axis.
In this case.
So if we go back to have a look at the compass NATO for a moment let's bring it in and you can see that
the axis is this one here facing that way.
So you want the compass to move in this direction and rotate around it.
So that means we then use the heading with a could turn in dark oil to zero in x zero or in the Y and
then we give hating to the actual rotation and that's going to rotate it around because the Euler angles
are going to work between 0 and 360 for a full turn around a circle or rotation.
Now if your compass.
If you have a compass needle it has a different axis of rotation.
Then you will need to change these so if it was rotating around its why for example then you put heading
in this spot here.
If Back here in the saying if this Zed axis.
So the camera is here and a compass needle has the Xerox's is facing away from the camera.
In this case if this was turned around so that the xit axis was facing towards the camera then you'd
have to write tight for a negative heading in order to get the compass needle always facing north.
In this cases which is what we're doing which is what compass needles do.
So that's important.
Now that's all.
OK so that's what three or four lines of code you need to add into your GP control.
So save that and then go back into unity and what you want to do is to remember to click on the piece
and drag your compass needle over to.
Now you can run us in the editor.
But of course there's no compass in here.
So we're not going to see anything.
We will get a heading of 0 showing that way pointing straight ahead so when your phone or mobile device
is upright like this that you're looking at it then and you rotate towards north then you will get the
compass needle going straight up.
Now when you run these on your mobile phone when you're facing north so that you just said you'll have
a heading of 0 and the compass needle will be facing straight up.
The idea behind the compass is that the needle itself always faces north.
So as you move around the compass needle actually moves as well but not with you with the magnetic force.
The earth so that if you turn to face east it will give you a heading of 90 from the measurements that
you're obtaining in your code.
But then the compass needle will appear to be pointing to the left on your device because it's actually
still trying to point north when you turn south.
You're heading will be one hundred eighty.
And in that case the compass needle will be facing directly towards you if you're holding device in
front of you.
And then when you turn west you'll have a heading of 270 and the compass needle will still be facing
north.
But on your device it will be facing to the right.
Now these heading values that I mentioned they're the perfect conditions when you're actually using
this you'll find that the compass values the heading values tend to Gigia around those optimal values.
So if you're going to test if you're facing in any particular direction you should always make sure
that you give it a range of values.
So instead of say ninety four aist heading you could say if I'm facing light between 80 and 100 instead.
Ok it's time for a challenge.
What I'd like to do is program a virtual compass.
I've given you all this sorry models and also the marker and WJC file as a zip file attached to his
lectures called Directions dot zip.
And I'd like it to display the direction of the compass rating on the mark or tag depending on which
way you've got your phone facing and looking at the marker.
Now the actual little augmented characters that you see on the tag should only appear when you're within
20 degrees of the true heading.
So for example for North when you're on 10 degrees on either side.
So I'll let you go off and do that.
And when you're finished come back and stop the next lecture which will show you the solution.