WEBVTT

00:03.800 --> 00:10.370
Believe it or not, at this point in the course, you've got enough mathematical knowledge to create

00:10.370 --> 00:19.820
an autopilot tank, which means that you can get the tank to turn and move towards the fuel can or by

00:19.820 --> 00:20.600
itself.

00:20.630 --> 00:23.160
So this is your challenge.

00:23.180 --> 00:24.650
I'd like you to program in it.

00:24.660 --> 00:33.980
So if you hit the T key, it turns on the autopilot that will have the tank navigate itself to the fuel

00:33.980 --> 00:34.280
tank.

00:34.280 --> 00:39.980
So don't forget the angle, the speed, the moving that you need to have the tank testing, whether

00:39.980 --> 00:46.160
the tanks actually got within a certain distance from being at the fuel tank in which it should then

00:46.160 --> 00:47.770
stop, of course.

00:47.780 --> 00:52.160
So pause the video now, go off and complete that challenge.

00:52.160 --> 00:55.400
And when we come back, I'll step through putting that together with you.

00:56.300 --> 00:57.110
Welcome back.

00:57.110 --> 00:59.390
How did you go getting that to work?

00:59.390 --> 01:04.550
Well, first of all, let's start by working on an autopilot method.

01:04.640 --> 01:10.640
So let's put void auto pilot like this.

01:10.850 --> 01:14.000
And what it's going to do is calculate the angle.

01:14.000 --> 01:15.620
So calculate angle.

01:15.620 --> 01:23.240
Now remember that calculate angle has that snapping ability already in it, which is down here.

01:24.080 --> 01:29.510
So that was as soon as we run this, it will calculate the angle and turn towards it, which is okay,

01:29.510 --> 01:30.890
we'll leave it like that for now.

01:30.920 --> 01:35.900
The other thing we want to do is actually push the tank forward.

01:35.900 --> 01:42.770
This is going to turn the tank and then we just need to keep pushing it forward along its y axis.

01:42.770 --> 01:46.160
Remember, it's the Y in this case that's going forward.

01:46.160 --> 01:52.430
So if we select our tank and hit the W key, we can see we have got that green one there, which is

01:52.430 --> 01:55.100
the most important part for pushing things forward.

01:55.370 --> 02:06.710
So then let's just go this dot trans form dot position and we can just update the position with a new

02:06.710 --> 02:10.830
vector three that moves the Y coordinate only.

02:10.850 --> 02:15.890
So let's say zero and then we need a small little value in here.

02:15.890 --> 02:21.770
Let's go 0.1 F to start with zero like that which will push it forward.

02:21.770 --> 02:28.040
Now remember that that forward vector for the tank is its y axis.

02:28.040 --> 02:30.710
Okay, so that's some kind of autopilot.

02:30.710 --> 02:39.230
Now we're going to go up the top here and just add in a bool autopilot equals false initially.

02:39.320 --> 02:53.450
Then we'll come down into our late up date and we're going to test if input dot get key down key code

02:54.350 --> 03:04.250
dot t in this case and that's going to make sure we've got an extra bracket there.

03:04.250 --> 03:16.430
And then down in here, then down in here we will go auto pilot equals not autopilot, which will turn

03:16.460 --> 03:17.510
it on and off.

03:17.960 --> 03:23.270
Then outside of here will go if autopilot.

03:23.270 --> 03:31.070
So if this is true every time this updates is going to run autopilot like that.

03:31.070 --> 03:32.900
Okay, so save this.

03:32.900 --> 03:36.560
Let's go back into unity and have a look at what we get here.

03:37.010 --> 03:44.660
So we're going to press play and then we will hit the T key and that will turn us and start moving the

03:44.660 --> 03:45.590
tank.

03:46.010 --> 03:55.970
So T now what's happening here is notice the tank is constantly turning towards the fuel tank and you'll

03:55.970 --> 04:01.820
notice my debug log lines are still in there, which is creating interesting patterns, but it's moving

04:01.820 --> 04:02.390
this way.

04:02.390 --> 04:09.380
That's because of the way that we're adding in the vector to our tanks position, because we're actually

04:09.380 --> 04:12.680
adding in a y vector to the tank position.

04:12.680 --> 04:20.930
So it's actually going to move in the world space instead of adding that 010 vector, which is this

04:20.930 --> 04:25.880
one here, we actually want to add whatever the forward facing direction of the tank is.

04:25.880 --> 04:29.570
Now, remember, that's going to be the up vector in the case of the tank.

04:29.570 --> 04:32.180
So let's just tweak that a little bit.

04:32.180 --> 04:39.470
So back in our code, if we go up to the top here, we're going to instead of adding the up vector,

04:39.530 --> 04:47.180
which is the world's vector or a vector facing straight up and down, we're going to add to it the this

04:47.180 --> 04:54.620
dot transform dot up, which you will know from experience now that this is a length of one.

04:54.620 --> 04:59.870
So if we want to include a speed as well, we would add another float at the top.

05:00.890 --> 05:03.230
And let's set the speed to.

05:03.530 --> 05:14.210
3.1 if and then we would multiply this by speed and we also need to multiply it by time, delta time

05:14.210 --> 05:20.300
if we want to take into consideration how fast we're actually moving as well.

05:20.300 --> 05:25.760
Now the speed I'm getting error here because I've actually got a different sort of speed for our drive,

05:25.760 --> 05:31.370
which was when we're driving it forward, let's just call this T speed for tank speed just to get rid

05:31.370 --> 05:36.020
of that error and let's save it and have a look at what we get now.

05:36.290 --> 05:44.090
So we'll press play and then we'll press the T, which should start us on our way.

05:44.120 --> 05:46.220
Okay, so there we go.

05:46.220 --> 05:50.150
Now what's going on with this is obviously the speed.

05:50.150 --> 05:56.240
You can tell it's moving very, very slightly, but we are heading off in the right direction, but

05:56.540 --> 06:01.820
only very slightly because of that multiplied by 0.1 for the speed.

06:02.270 --> 06:04.610
So we can actually easily fix that up.

06:04.610 --> 06:12.890
The other issue, which you will see once we do put this up to one, is that we have this jittery back

06:12.890 --> 06:20.300
and forth thing going on for the tank, and that's because it's trying to get perfectly aligned with

06:20.300 --> 06:22.100
the actual fuel tank.

06:22.460 --> 06:24.460
So now we're moving a little bit faster.

06:24.470 --> 06:30.800
You probably could multiply that by two, but you can tell if we zoom in a bit closer that as we update

06:30.800 --> 06:36.620
our angle back and forward, we're actually going back and forward by like this amount on each side

06:36.620 --> 06:39.980
of that vector towards the fuel.

06:40.370 --> 06:47.390
This is quite a common issue that you get with using maths, which is very precise in a application

06:47.390 --> 06:53.300
such as this because as you start moving your objects around, your calculations are giving you precise

06:53.300 --> 06:56.510
values, but you don't necessarily want precise values.

06:56.510 --> 07:03.380
And you saw that when we were trying to move our pigs towards the villager or a goal location.

07:03.560 --> 07:08.150
We want the sort of near enough is good enough scenario going on with these.

07:08.150 --> 07:10.370
We don't want it to be absolutely perfect.

07:10.370 --> 07:15.800
We also don't really want the tank to snap around in one go towards the fuel.

07:15.800 --> 07:23.720
So instead we're going to add a turn speed in there as well, just like we have a forward speed.

07:24.230 --> 07:24.590
All right.

07:24.590 --> 07:27.440
So we have our speed for our tank.

07:27.440 --> 07:32.450
Now, let's make that, too, because it was a little bit slow and then we'll have a rotation speed

07:32.450 --> 07:33.020
as well.

07:33.020 --> 07:34.790
So let's call it our speed.

07:34.790 --> 07:39.950
We have a rotation speed for when we're using the keys, which is up here, but this is for our autopilot.

07:40.100 --> 07:49.610
So let's make that 0.5 F and of course you can also multiply that by time delta time in the rotation

07:49.610 --> 07:50.390
values.

07:50.390 --> 07:52.310
But I'm not going to put that in here.

07:52.310 --> 08:00.950
But you can so we come down to the rotate for our clockwise and we add in here our our speed value to

08:00.950 --> 08:03.440
multiply it in there.

08:03.620 --> 08:12.110
The other thing we also need to do is actually turn off autopilot when we get close enough to our fuel.

08:12.110 --> 08:16.430
So we'll do that down in our LT update.

08:16.430 --> 08:19.760
We already have got the ability to calculate distance.

08:19.880 --> 08:28.100
Let's go down here and what we're going to do under here is go if calculate distance and we'll say if

08:28.100 --> 08:38.030
that threshold is less than let's go with two at this point then we will go auto pilot equals false

08:38.030 --> 08:39.410
and just turn it off.

08:39.650 --> 08:43.490
Calculate distance is not returning anything at the moment, but that's okay.

08:43.490 --> 08:44.150
We can fix that.

08:44.150 --> 08:50.540
So if we come up to calculate distance, we can then return distance in here.

08:51.720 --> 08:52.530
Like that.

08:52.800 --> 08:53.410
Okay.

08:53.430 --> 08:55.320
And make sure this returns afloat.

08:56.710 --> 08:57.000
Right.

08:57.000 --> 09:00.300
So let's say this and try it out.

09:01.020 --> 09:07.170
So what we're going to do is have our tank moving and rotating a little bit by a little bit towards

09:07.170 --> 09:08.310
the fuel.

09:08.880 --> 09:12.090
And it's also going to turn off its autopilot when it gets there.

09:12.090 --> 09:15.360
So if we hit the T key, you'll see now that it's coming back.

09:15.360 --> 09:18.930
Now it's jittering a little bit, not as much as it was before.

09:18.930 --> 09:24.240
And you could even change that rotation, speed down even further.

09:24.270 --> 09:30.630
You could also do a sort of test as if the angle is greater than whatever.

09:30.630 --> 09:34.050
Then don't bother performing a turn.

09:34.380 --> 09:42.000
So we should get close to having our autopilot turned off very shortly, which we aren't, of course.

09:42.000 --> 09:43.470
So let's have a look at our console.

09:43.470 --> 09:48.060
Let's pause this and have a look at the distance value that is being put out here.

09:48.420 --> 09:54.180
So this is a perfect situation where you'll get this sort of accuracy problem.

09:54.180 --> 09:58.590
Notice the distance is 2.00083.

09:58.590 --> 10:01.500
And I said, when it's less than two, that's not less than two.

10:01.500 --> 10:06.690
So this is how accurate and close these values can be that might cause you issues.

10:06.690 --> 10:09.990
So let's say less than say three.

10:09.990 --> 10:11.790
So let's go back into our code.

10:11.790 --> 10:15.690
We'll change that in our autopilot for less than three.

10:15.840 --> 10:20.970
And I'll also put that turning code in here just so you know how that might work.

10:20.970 --> 10:27.030
If we come up to, let's say, calculate angle, this is where our rotate is happening.

10:27.180 --> 10:37.200
Let's say that if the angle greater than ten degrees, remembering that the angle is in radians, we

10:37.200 --> 10:44.820
actually need to make sure we multiply that before we testing it against our ten, which is in degrees.

10:45.360 --> 10:47.670
Then we will turn.

10:47.670 --> 10:49.920
Now this are speed that I've got here.

10:49.920 --> 10:54.930
I'm just going to come back up and make it a little bit smaller so that you can actually see the effect

10:54.930 --> 11:02.280
of having that smaller and that you'll get a gradual turn in the object rather than just a snapping

11:02.280 --> 11:03.720
around to face it.

11:03.870 --> 11:07.320
Okay, so let's try this out.

11:07.350 --> 11:14.040
You have to remember that as you're sort of tweaking these values, we can always end up with other

11:14.040 --> 11:18.180
things that will go wrong, such as the distance to an object.

11:18.180 --> 11:20.850
And in fact, let me hit t here.

11:20.880 --> 11:23.130
Did you notice how that slowly turned around?

11:23.130 --> 11:29.970
If I hit T again to turn off autopilot and come up out to this way for hit t, it turns around.

11:29.970 --> 11:32.010
Okay, rather than snapping around.

11:32.040 --> 11:39.600
Now, as you make it more difficult for the tank to turn in one go like if it only can do really big

11:39.600 --> 11:46.710
steep turns, if it's very close to an object, it may never, ever be able to turn in to be close enough

11:46.710 --> 11:54.090
to an object, in which case you will end up with all sorts of sort of inaccuracies in the system.

11:54.330 --> 11:56.250
So just be aware of that.

11:56.250 --> 12:02.400
You might have to increase the distance threshold to stopping and you can see my tank has now stopped.

12:02.400 --> 12:09.660
So increase that as you make the angle that a can turn greater as well because it's only really sharp

12:09.660 --> 12:14.040
turning objects that are going to be able to hit the target that thereafter.

12:14.040 --> 12:20.160
So now it's autopilot turned off I should be able to sort of drive away and then if I can hit the T

12:20.160 --> 12:27.060
key again, you'll see I come back and slightly adjusting every time you get slightly out on that angle

12:27.060 --> 12:29.040
and come back in towards the fuel.

12:29.070 --> 12:29.330
Okay.

12:29.340 --> 12:36.000
So there's a very simple autopilot application of the mathematics that we've been learning about in

12:36.000 --> 12:37.020
this section.

12:37.020 --> 12:44.880
And believe it or not, the AI that we use in games today is pretty much based on what you've just seen

12:45.060 --> 12:49.260
with a few more tweaks and lots of animations and special effects and that type of thing.

12:49.260 --> 12:55.830
But it's really about the underlying vectors and that mathematics that just make this stuff work.
