Believe it or not, at this point in the course, you've got enough mathematical knowledge to create
an autopilot tank, which means that you can get the tank to turn and move towards the fuel can or by
itself.
So this is your challenge.
I'd like you to program in it.
So if you hit the T key, it turns on the autopilot that will have the tank navigate itself to the fuel
tank.
So don't forget the angle, the speed, the moving that you need to have the tank testing, whether
the tanks actually got within a certain distance from being at the fuel tank in which it should then
stop, of course.
So pause the video now, go off and complete that challenge.
And when we come back, I'll step through putting that together with you.
Welcome back.
How did you go getting that to work?
Well, first of all, let's start by working on an autopilot method.
So let's put void auto pilot like this.
And what it's going to do is calculate the angle.
So calculate angle.
Now remember that calculate angle has that snapping ability already in it, which is down here.
So that was as soon as we run this, it will calculate the angle and turn towards it, which is okay,
we'll leave it like that for now.
The other thing we want to do is actually push the tank forward.
This is going to turn the tank and then we just need to keep pushing it forward along its y axis.
Remember, it's the Y in this case that's going forward.
So if we select our tank and hit the W key, we can see we have got that green one there, which is
the most important part for pushing things forward.
So then let's just go this dot trans form dot position and we can just update the position with a new
vector three that moves the Y coordinate only.
So let's say zero and then we need a small little value in here.
Let's go 0.1 F to start with zero like that which will push it forward.
Now remember that that forward vector for the tank is its y axis.
Okay, so that's some kind of autopilot.
Now we're going to go up the top here and just add in a bool autopilot equals false initially.
Then we'll come down into our late up date and we're going to test if input dot get key down key code
dot t in this case and that's going to make sure we've got an extra bracket there.
And then down in here, then down in here we will go auto pilot equals not autopilot, which will turn
it on and off.
Then outside of here will go if autopilot.
So if this is true every time this updates is going to run autopilot like that.
Okay, so save this.
Let's go back into unity and have a look at what we get here.
So we're going to press play and then we will hit the T key and that will turn us and start moving the
tank.
So T now what's happening here is notice the tank is constantly turning towards the fuel tank and you'll
notice my debug log lines are still in there, which is creating interesting patterns, but it's moving
this way.
That's because of the way that we're adding in the vector to our tanks position, because we're actually
adding in a y vector to the tank position.
So it's actually going to move in the world space instead of adding that 010 vector, which is this
one here, we actually want to add whatever the forward facing direction of the tank is.
Now, remember, that's going to be the up vector in the case of the tank.
So let's just tweak that a little bit.
So back in our code, if we go up to the top here, we're going to instead of adding the up vector,
which is the world's vector or a vector facing straight up and down, we're going to add to it the this
dot transform dot up, which you will know from experience now that this is a length of one.
So if we want to include a speed as well, we would add another float at the top.
And let's set the speed to.
3.1 if and then we would multiply this by speed and we also need to multiply it by time, delta time
if we want to take into consideration how fast we're actually moving as well.
Now the speed I'm getting error here because I've actually got a different sort of speed for our drive,
which was when we're driving it forward, let's just call this T speed for tank speed just to get rid
of that error and let's save it and have a look at what we get now.
So we'll press play and then we'll press the T, which should start us on our way.
Okay, so there we go.
Now what's going on with this is obviously the speed.
You can tell it's moving very, very slightly, but we are heading off in the right direction, but
only very slightly because of that multiplied by 0.1 for the speed.
So we can actually easily fix that up.
The other issue, which you will see once we do put this up to one, is that we have this jittery back
and forth thing going on for the tank, and that's because it's trying to get perfectly aligned with
the actual fuel tank.
So now we're moving a little bit faster.
You probably could multiply that by two, but you can tell if we zoom in a bit closer that as we update
our angle back and forward, we're actually going back and forward by like this amount on each side
of that vector towards the fuel.
This is quite a common issue that you get with using maths, which is very precise in a application
such as this because as you start moving your objects around, your calculations are giving you precise
values, but you don't necessarily want precise values.
And you saw that when we were trying to move our pigs towards the villager or a goal location.
We want the sort of near enough is good enough scenario going on with these.
We don't want it to be absolutely perfect.
We also don't really want the tank to snap around in one go towards the fuel.
So instead we're going to add a turn speed in there as well, just like we have a forward speed.
All right.
So we have our speed for our tank.
Now, let's make that, too, because it was a little bit slow and then we'll have a rotation speed
as well.
So let's call it our speed.
We have a rotation speed for when we're using the keys, which is up here, but this is for our autopilot.
So let's make that 0.5 F and of course you can also multiply that by time delta time in the rotation
values.
But I'm not going to put that in here.
But you can so we come down to the rotate for our clockwise and we add in here our our speed value to
multiply it in there.
The other thing we also need to do is actually turn off autopilot when we get close enough to our fuel.
So we'll do that down in our LT update.
We already have got the ability to calculate distance.
Let's go down here and what we're going to do under here is go if calculate distance and we'll say if
that threshold is less than let's go with two at this point then we will go auto pilot equals false
and just turn it off.
Calculate distance is not returning anything at the moment, but that's okay.
We can fix that.
So if we come up to calculate distance, we can then return distance in here.
Like that.
Okay.
And make sure this returns afloat.
Right.
So let's say this and try it out.
So what we're going to do is have our tank moving and rotating a little bit by a little bit towards
the fuel.
And it's also going to turn off its autopilot when it gets there.
So if we hit the T key, you'll see now that it's coming back.
Now it's jittering a little bit, not as much as it was before.
And you could even change that rotation, speed down even further.
You could also do a sort of test as if the angle is greater than whatever.
Then don't bother performing a turn.
So we should get close to having our autopilot turned off very shortly, which we aren't, of course.
So let's have a look at our console.
Let's pause this and have a look at the distance value that is being put out here.
So this is a perfect situation where you'll get this sort of accuracy problem.
Notice the distance is 2.00083.
And I said, when it's less than two, that's not less than two.
So this is how accurate and close these values can be that might cause you issues.
So let's say less than say three.
So let's go back into our code.
We'll change that in our autopilot for less than three.
And I'll also put that turning code in here just so you know how that might work.
If we come up to, let's say, calculate angle, this is where our rotate is happening.
Let's say that if the angle greater than ten degrees, remembering that the angle is in radians, we
actually need to make sure we multiply that before we testing it against our ten, which is in degrees.
Then we will turn.
Now this are speed that I've got here.
I'm just going to come back up and make it a little bit smaller so that you can actually see the effect
of having that smaller and that you'll get a gradual turn in the object rather than just a snapping
around to face it.
Okay, so let's try this out.
You have to remember that as you're sort of tweaking these values, we can always end up with other
things that will go wrong, such as the distance to an object.
And in fact, let me hit t here.
Did you notice how that slowly turned around?
If I hit T again to turn off autopilot and come up out to this way for hit t, it turns around.
Okay, rather than snapping around.
Now, as you make it more difficult for the tank to turn in one go like if it only can do really big
steep turns, if it's very close to an object, it may never, ever be able to turn in to be close enough
to an object, in which case you will end up with all sorts of sort of inaccuracies in the system.
So just be aware of that.
You might have to increase the distance threshold to stopping and you can see my tank has now stopped.
So increase that as you make the angle that a can turn greater as well because it's only really sharp
turning objects that are going to be able to hit the target that thereafter.
So now it's autopilot turned off I should be able to sort of drive away and then if I can hit the T
key again, you'll see I come back and slightly adjusting every time you get slightly out on that angle
and come back in towards the fuel.
Okay.
So there's a very simple autopilot application of the mathematics that we've been learning about in
this section.
And believe it or not, the AI that we use in games today is pretty much based on what you've just seen
with a few more tweaks and lots of animations and special effects and that type of thing.
But it's really about the underlying vectors and that mathematics that just make this stuff work.