WEBVTT

00:02.990 --> 00:08.870
In this lecture, we're going to fix our waypoint following by adding in a tracker.

00:09.230 --> 00:09.680
Right?

00:09.680 --> 00:18.920
So in order to do that, you need to create an object that's going to follow the track around and then

00:18.920 --> 00:21.290
have your tank follow that object.

00:21.290 --> 00:29.540
So you want it like a super accurate progress tracker is what we're going to call it that stays on the

00:29.540 --> 00:34.190
track and then the tanks actually follow it instead.

00:34.940 --> 00:39.920
So go into your script and what we're going to do is create a new game object.

00:39.920 --> 00:47.390
So at the top we're going to declare it so game object and we'll call it our tracker and then we'll

00:47.390 --> 00:50.000
create this tracker down in the start.

00:50.000 --> 00:57.470
So the tracker is going to equal game object dot create primitive.

00:57.470 --> 01:03.860
Now I'm creating a primitive object so that you can actually see where the tracker is in unity.

01:03.860 --> 01:07.310
Of course you don't need to see this position.

01:07.310 --> 01:11.840
It's just a point that's going to be moving around the map, but so that you can visualize it.

01:11.840 --> 01:15.860
We will make it, I guess, a kind of solid object.

01:15.860 --> 01:20.060
So we're going to create the primitive of a cylinder.

01:20.060 --> 01:30.080
So just select that from the dropdown list if you're in Visual Studio and then we want to destroy immediately

01:31.010 --> 01:33.470
the collider that's on it.

01:33.470 --> 01:37.130
So tracker dot get component.

01:39.640 --> 01:40.570
Collider.

01:43.050 --> 01:45.220
And make sure that that's a capital G.

01:45.240 --> 01:48.270
For game object, create primitive, right?

01:48.270 --> 01:52.560
The next thing we're going to do is set its position and rotation.

01:52.560 --> 01:59.220
So tracker dot transform dot position equals.

02:00.130 --> 02:00.970
Our tank.

02:00.970 --> 02:04.360
So this dot transform dot.

02:05.210 --> 02:10.070
Position and the rotation tracker transform.

02:11.820 --> 02:18.240
Dot rotation equals this dot transform dot rotation.

02:20.250 --> 02:26.310
Now the tracker, we want to move around the track using the original method that we had for moving

02:26.310 --> 02:26.790
the tank.

02:26.790 --> 02:39.130
So let's create a Void Progress Tracker, and this method will actually replicate what we had previously.

02:39.150 --> 02:46.050
So first of all, it's going to test the distance of the tracker to the waypoint, and we've already

02:46.050 --> 02:48.000
got that code sitting down here in the update.

02:48.010 --> 02:51.330
So I'm just going to copy that as well as the length of waypoint.

02:51.330 --> 02:55.170
So let's copy that out of there and put it down there.

02:55.620 --> 03:03.000
And then what we're going to do is actually perform a look at again for it so that it is perfectly going

03:03.000 --> 03:04.560
towards the tree.

03:05.100 --> 03:07.170
So that would be tracker.

03:10.320 --> 03:11.940
Don't transform.

03:12.150 --> 03:12.900
Look at.

03:14.470 --> 03:22.390
And we're going to look at the waypoints of the current waypoint.

03:26.690 --> 03:28.460
To transform.

03:30.640 --> 03:34.360
And then we're also going to move it forward.

03:34.360 --> 03:45.820
So tracker dot transform dot translate zero zero and 0.1.

03:45.850 --> 03:55.180
If this speed here, you can modify that based on how fast you want your tracker to go around the track.

03:55.180 --> 04:01.450
But what you've got to make sure that you do is your tank and your tracker can't get too far away from

04:01.450 --> 04:02.170
each other.

04:02.320 --> 04:07.720
Otherwise, the tank, which is going to be following the tracker, not the waypoints, is going to

04:07.720 --> 04:14.360
start skipping off the track and taking shortcuts, which is something that you definitely do not want.

04:14.380 --> 04:14.680
All right.

04:14.680 --> 04:23.860
So with that code, we're going to go down into our update and let's just get rid of this because we're

04:23.860 --> 04:30.250
not going to be using that again and we'll put in here progress.

04:31.070 --> 04:31.970
Trekker.

04:33.460 --> 04:35.920
And we'll also just.

04:37.210 --> 04:42.760
Comment out these lines of code just for now so that our tank doesn't go anywhere.

04:42.760 --> 04:45.100
It's our progress tracker that's going to be moving.

04:45.520 --> 04:51.460
And because it is the progress tracker that's moving, when we test for the distance in order to go

04:51.460 --> 04:55.660
to the next waypoint, it's actually a tracker that we have to test the distance for.

04:55.660 --> 05:04.750
So back up in progress tracker, make sure that you change that to tracker transform position is less

05:04.750 --> 05:09.160
than three otherwise it's not going to continue around the track.

05:09.280 --> 05:09.700
Right?

05:09.700 --> 05:10.570
So save it.

05:10.570 --> 05:12.880
Let's go back into unity now.

05:12.880 --> 05:15.520
Once you're in there, you can see that I've already done this.

05:15.520 --> 05:18.400
I've just left one green tank active.

05:18.400 --> 05:26.050
So go through and select all of these other tanks that you've got in the scene and just tick their box

05:26.050 --> 05:29.470
that's right at the top of the inspector, which will just turn them off for now because they've all

05:29.470 --> 05:31.870
got exactly the same code on them.

05:31.870 --> 05:33.640
And let's press play.

05:33.640 --> 05:40.240
We'll see that the cylinder gets created and off goes our cylinder on its merry way around the track.

05:40.240 --> 05:46.180
Now, because it's turning and perfectly looking at the palm tree or the next waypoint, it's going

05:46.180 --> 05:52.090
to always hit it and it's still got a distance of three because it is perfectly turning towards the

05:52.090 --> 05:53.170
position of the palm tree.

05:53.170 --> 05:59.770
You could probably turn that down a little bit more, so you've got even better accuracy.

05:59.800 --> 06:06.700
Okay, so you've got our tracker now we want to get our tank to follow that tracker instead.

06:07.700 --> 06:08.150
Okay.

06:08.150 --> 06:14.660
So back into our code and we're now not even looking at anything so we can get rid of that line that

06:14.660 --> 06:16.340
we commented out much earlier.

06:16.640 --> 06:21.560
Our concern in Look at Waypoint is actually now not looking at a waypoint.

06:21.560 --> 06:22.790
It's going to look at the tracker.

06:22.790 --> 06:25.490
So you want to change this to tracker here.

06:25.820 --> 06:34.940
Then we can do our slurp of our tanks rotation, which is fine, and then we can push our tank forward

06:34.940 --> 06:35.960
by the speed.

06:35.990 --> 06:38.630
Okay, so that is fine as well.

06:38.630 --> 06:43.400
So let's just save that and go back to Unity and have another little look.

06:43.400 --> 06:47.720
We'll create our cylinder that's going to move along.

06:49.930 --> 06:55.780
And you can see that the tank is now circling our cylinder.

06:55.870 --> 06:57.220
Why is it doing that?

06:57.220 --> 06:57.860
Well.

06:57.880 --> 07:01.600
It's moving much faster than the cylinder is.

07:01.600 --> 07:09.250
So therefore, we have to make our cylinder move faster than the tank back into our code.

07:09.370 --> 07:15.340
So the progress checker we've now learnt needs to go at least as fast as the tank itself.

07:15.340 --> 07:25.540
So let's put speed multiplied by time, delta time in here and let's just say speed, let's just increase

07:25.540 --> 07:26.380
it a little bit.

07:26.380 --> 07:31.960
So it's in front speed plus say two like that.

07:31.960 --> 07:37.150
And because it's not actually turning, it should stay ahead quite nicely of the tank.

07:37.160 --> 07:37.870
Save it.

07:38.080 --> 07:43.270
Let's go back in and play again and have a look what we're getting.

07:43.270 --> 07:45.760
So the off it goes.

07:47.380 --> 07:50.890
Now you can see that the tank is following it nicely.

07:52.560 --> 07:53.640
And that's not too bad.

07:53.670 --> 07:59.610
It's taking a lot of shortcuts because of the actual distance between them.

08:01.430 --> 08:05.080
So you could change that distance.

08:05.090 --> 08:06.770
So that is a little bit shorter.

08:06.780 --> 08:13.670
Now, what happens if your cylinder ends up going too fast for your tank?

08:13.670 --> 08:20.810
So if the tank gets knocked out of the way by another vehicle and it ends up way away from it, it could

08:20.810 --> 08:27.050
actually, instead of getting back on the track, take a shortcut straight across the actual track.

08:27.290 --> 08:31.010
Let's go back in here and let's increase our speed.

08:31.010 --> 08:39.200
Let's make it say 20 faster than the tank and go back and run that and have a look at the result of

08:39.200 --> 08:42.650
your tank going around these trees.

08:42.650 --> 08:42.910
Okay.

08:42.920 --> 08:50.210
So now you can see that the tank is taking massive shortcuts around the trees because this tracker is

08:50.210 --> 08:54.050
just getting so much further ahead.

08:54.800 --> 08:59.870
So we actually want to reduce the tracker from getting too far ahead of the tank.

08:59.900 --> 09:00.290
Right.

09:00.290 --> 09:07.190
So back into the code, what we're going to do at the top is create another public float and we will

09:07.190 --> 09:09.860
call it look ahead.

09:10.010 --> 09:16.640
And we're going to say how far ahead of the tank will we actually let the tracker get?

09:16.850 --> 09:23.150
So let's say that it's ten, ten, which is my go to value.

09:23.390 --> 09:31.940
And then down in the progress tracker, we're not going to let it move any further unless it is within

09:31.940 --> 09:34.490
ten of the tank itself.

09:34.490 --> 09:43.220
So we'll do another distance test here if vector three dot distance between the tracker.

09:44.420 --> 09:57.890
Don't transform position and this dot transform dot position greater than look ahead and an easy return

09:57.890 --> 09:58.760
out of that.

09:59.330 --> 10:00.920
So let's save it.

10:01.280 --> 10:05.990
Go back and we'll have a look at what we get when we press play.

10:07.210 --> 10:08.620
So the trekker.

10:09.630 --> 10:12.840
You can see now and you saw it at the beginning.

10:12.840 --> 10:16.710
Let's just get closer into here and have a look.

10:17.220 --> 10:21.420
Is it will stop and wait for the tank to catch up to it.

10:24.060 --> 10:25.410
And you saw that there.

10:26.400 --> 10:33.060
So it's never no matter how fast you make your tracker, going to get beyond the tank, but you can

10:33.060 --> 10:36.930
adjust it to be even further away if you want to.

10:36.960 --> 10:43.320
So now that you've got that tracker on the code, it's now going to be on all of your other tanks.

10:43.320 --> 10:44.250
So you can actually.

10:45.730 --> 10:48.730
Put these tanks back into action.

10:49.830 --> 11:00.750
And press play, and now you won't get any tanks that will circle their waypoints because they're following

11:00.750 --> 11:03.660
these sort of perfect trackers.

11:04.960 --> 11:09.610
The last thing, of course, that you need to do now is just go back into your code and make sure that

11:09.610 --> 11:14.080
that cylinder doesn't show up in the scene.

11:14.080 --> 11:17.230
So in here, what we're going to do is put tracker.

11:18.340 --> 11:20.800
Don't get component.

11:24.940 --> 11:30.490
Mesh renderer enabled equals false just to turn off.

11:31.340 --> 11:33.350
That cylinder so that you can't see it.

11:33.830 --> 11:36.620
Save that and press play.

11:36.920 --> 11:45.140
And now we have a very basic set of waypoints, a circuit of waypoints and some different vehicles that

11:45.140 --> 11:47.930
go around there, including the physics.

11:48.020 --> 11:54.530
There's a little bit of sliding going on and some of the vehicles are being knocked out of place.

11:54.680 --> 12:00.560
But that's up to you to experiment with now so you can change the rigid body settings, make it a heavier

12:00.560 --> 12:04.000
vehicle so that when they crash they will actually go flying.

12:04.010 --> 12:09.350
You might even want to remove the other physics restrictions that we've got on freezing the rotation

12:09.350 --> 12:11.120
just to see how that will work.

12:11.120 --> 12:19.490
But what you've now got is a good way of creating eyes that go around a circuit in a more believable

12:19.490 --> 12:23.330
fashion than ones that sit there and circle around the waypoints.
