WEBVTT

0
00:12.410 --> 00:19.220
In this lecture, we will start looking at one of the most fundamental A.I. techniques used in navigation: Waypoints.

1
00:19.500 --> 00:24.720
Waypoints are simply positions on a map that, when navigated in sequence, produce a path.

2
00:24.720 --> 00:27.150
It's the same system used in a car G.P.S.

3
00:27.180 --> 00:33.480
that gives you instructions on where to drive. Waypoints are an easy system to implement in an NPC as

4
00:33.480 --> 00:35.230
it makes them look more intelligent than they

5
00:35.250 --> 00:35.830
are

6
00:36.090 --> 00:42.860
and by providing them with a safe set of locations on a map to move between, they can also avoid obstacles.

7
00:42.990 --> 00:48.000
We've already created an NPC that can move and follow a goal location. Updating this to a waypoint

8
00:48.000 --> 00:50.700
system is straightforward. To follow along,

9
00:50.700 --> 00:56.310
download the project associated with this lecture and open the tank scene. In the scene, you'll find a little

10
00:56.310 --> 00:57.140
purple tank

11
00:57.180 --> 01:02.700
and it's this little purple tank that will be our NPC. To begin, we need to create some

12
01:02.700 --> 01:03.420
waypoints

13
01:03.570 --> 01:07.600
and I usually create my waypoints from spheres so that I can see where they are.

14
01:07.650 --> 01:15.820
So over in the Hierarchy, right-click and go 3D Object > Sphere and that will create a sphere for you.

15
01:15.850 --> 01:20.960
I'm also going to color it in red. There's a red material in this project file.

16
01:21.000 --> 01:23.040
This is just so that we can see it

17
01:23.280 --> 01:30.700
and you also want to take off the Sphere Collider as well because you don't want it to be a physics object.

18
01:30.780 --> 01:36.240
All this is, is a marker for our tank to navigate around.

19
01:36.300 --> 01:41.750
Later on, you may even turn off your mesh renderer when you don't want to see the waypoints, but when you're

20
01:41.760 --> 01:48.240
editing them it's good if you know where they are 'exactly', rather than just a transform.

21
01:48.240 --> 01:50.480
Right, so this is a waypoint

22
01:50.580 --> 01:58.170
and in order to make it a waypoint, I've created a tag and you'll need to assign this tag to this sphere,

23
01:58.170 --> 02:02.880
so just go over to the Inspector and look through the list and you'll find 'waypoint'.

24
02:02.880 --> 02:09.000
If it's not there (or you're creating your own system) you'll need to add a tag and then come back and

25
02:09.100 --> 02:12.300
reassign your waypoint tag to this sphere.

26
02:12.420 --> 02:14.370
Take notice also how it's spelled

27
02:14.370 --> 02:16.270
because we're going to search for it later.

28
02:16.280 --> 02:19.110
So this is 'waypoint' all in lowercase.

29
02:19.380 --> 02:21.060
So there's our first waypoint.

30
02:21.060 --> 02:29.850
Now with that created, you can move it into place and then Ctrl-D and make multiple copies of it

31
02:29.970 --> 02:33.680
and make a little circuit for the tank to move around.

32
02:34.590 --> 02:37.400
It doesn't even need to be perfectly round.

33
02:38.660 --> 02:41.590
Just go with something like that.

34
02:41.600 --> 02:45.410
Next, we'll create a script that will follow the waypoints.

35
02:45.410 --> 02:50.980
So go to Project > Create > C# Script...let's call this WaypointFollow

36
02:53.700 --> 02:58.510
and open it up and we'll add in the following code.

37
02:59.510 --> 03:05.630
The code includes a lot of the stuff that we've done before and that we did with our zombie example of

38
03:05.630 --> 03:09.570
moving her around and following and slerping.

39
03:09.620 --> 03:13.180
So you'll see those same dynamics in a moment when we get down there.

40
03:13.370 --> 03:20.990
First of all, though, you need an array of game objects to hold all of those waypoints and we're going

41
03:20.990 --> 03:23.600
to populate that automatically.

42
03:23.600 --> 03:27.410
Next, an 'int' which is the current waypoint.

43
03:27.410 --> 03:34.340
So it's the index into this waypoint array for the sphere we're currently heading towards.

44
03:34.370 --> 03:39.890
After that, you've got the usual suspects that we've had before with speed, accuracy and rotational

45
03:39.890 --> 03:41.220
speed.

46
03:41.270 --> 03:47.900
Now in the Start function, we're setting the waypoint array to equal all of the game objects that

47
03:47.900 --> 03:50.270
have the tag 'waypoint'.

48
03:50.270 --> 03:55.800
So this is automatically filling up this waypoint array for you.

49
03:56.210 --> 04:03.350
If you wanted to specify the waypoints yourself, you could manually drop them into this waypoint

50
04:03.380 --> 04:09.540
array through the Inspector and then you could put them in the order that you want them in.

51
04:09.590 --> 04:15.720
But for now, we're just doing this automatically because it's just way quicker. In the LateUpdate is

52
04:15.720 --> 04:20.880
where all of the work is going to happen and you can see it's quite short really.

53
04:20.940 --> 04:25.500
First of all, we're going to check if we have any waypoints. If we don't, then we're just going

54
04:25.500 --> 04:28.820
to return because there's no point moving anywhere.

55
04:29.160 --> 04:33.330
Then we have our lookAtGoal being constructed again.

56
04:33.380 --> 04:42.290
Again, this is a 2D kind of LookAt, as far as it being at the same height as the tank

57
04:42.570 --> 04:50.820
but in the x and z of the waypoint. If you wanted a 3D system of waypoints, where you've got

58
04:50.820 --> 04:57.000
an airplane or you've got a bird or something that can move in three dimensions, then you would obviously

59
04:57.000 --> 05:04.120
put the waypoints y-position in here, instead of the tanks

60
05:04.140 --> 05:05.820
y-position.

61
05:05.820 --> 05:09.470
We then calculate our direction as before

62
05:09.690 --> 05:16.840
and then we perform the slerp, which will turn the tank towards the waypoint (or in that direction).

63
05:17.100 --> 05:17.370
Right.

64
05:17.370 --> 05:22.680
We then go ahead and test our magnitude for being 'less than' accuracy, to say whether we're close enough

65
05:22.680 --> 05:24.910
to that particular waypoint.

66
05:24.930 --> 05:31.380
If we are close enough to the particular waypoint, then we increase our waypoint that we're heading

67
05:31.380 --> 05:32.420
to by 1.

68
05:32.430 --> 05:35.430
So let's say we're heading to waypoint 0

69
05:35.550 --> 05:38.220
and then we've reached it within our accuracy,

70
05:38.220 --> 05:44.460
this will then go to waypoint 1 and that will cause us to start looking at waypoint 1 and moving

71
05:44.460 --> 05:46.280
to waypoint 1.

72
05:46.290 --> 05:46.670
All right.

73
05:46.680 --> 05:53.340
Now if we've reached the end of that array, we've run out of waypoints. Then we just go back to the first

74
05:53.340 --> 05:54.020
waypoint.

75
05:54.030 --> 06:01.880
So this is going to cycle around for us. After all that happens, we're then going to perform our Translate.

76
06:02.130 --> 06:09.750
Notice the Translate is not restricted in any way, it's just going to happen regardless of anything

77
06:09.750 --> 06:15.750
except there being no waypoints at all. So we're turning, we're looking at the waypoint we

78
06:15.750 --> 06:22.710
should be looking at and then we're pushing the tank forward because this whole update is about cycling

79
06:23.100 --> 06:27.480
around the waypoints, so the tank will never stop at anything.

80
06:27.660 --> 06:35.160
Save that, then go back to Unity and what you want to do is select your tank and drag-and-drop

81
06:35.160 --> 06:39.230
WaypointFollow on to the tank.

82
06:39.280 --> 06:43.570
Now in the Inspector, you'll see the waypoints here.

83
06:43.570 --> 06:50.220
Now if you wanted to put your waypoints in, you could drag-and-drop them, as I said before, from the Hierarchy

84
06:50.630 --> 06:55.760
on to that array and then put them in and change the order that you want

85
06:55.770 --> 07:00.930
but we're doing this automatically, so there's no need to in this case.

86
07:00.930 --> 07:08.400
So once you've done that, press Play and you'll find that the tank starts to follow your

87
07:08.400 --> 07:09.840
waypoints around.

88
07:09.890 --> 07:11.240
Ok, so it's off to this one...

89
07:14.120 --> 07:17.780
and then it will turn and go to this one...

90
07:17.800 --> 07:19.630
If this is a little bit slow for you,

91
07:19.630 --> 07:23.450
we just go back into the waypoints and modify the speed.

92
07:23.470 --> 07:25.510
Let's put this up to 3.

93
07:25.510 --> 07:30.600
Now if you increase the speed, you also need to increase the rotational speed as well,

94
07:30.640 --> 07:34.240
otherwise you'll find that you get a tank that does an

95
07:34.240 --> 07:38.320
awful lot of swinging around in big arcs.

96
07:38.670 --> 07:45.520
So let's see if this helps.

97
07:45.550 --> 07:46.280
Not too bad.

98
07:46.330 --> 07:52.240
You can see that it does take a quite a big arc to turn because that rotational speed's a bit slower

99
07:52.240 --> 07:54.430
than the actual forward speed, but it still works.

100
07:54.430 --> 07:57.160
It gets close enough to the points.

101
07:57.160 --> 08:06.190
Now remember, if you get a tank that starts to go around a waypoint or starts to jitter on a waypoint,

102
08:06.190 --> 08:11.260
it's because the accuracy is too low and you'll need to increase your accuracy.

103
08:11.260 --> 08:13.530
Let's see if we can force the tank to do that.

104
08:13.690 --> 08:20.740
Open up your script and the accuracy (which is currently 1), let's make it 0.5.

105
08:20.740 --> 08:24.440
Save that and go let's go back and run this

106
08:24.460 --> 08:31.040
and let's see, if the tank never reaches the waypoint, it will just start to circle the waypoint because

107
08:31.040 --> 08:39.650
it's constantly changing its direction vector to point at the sphere, whilst it's being pushed forward

108
08:39.680 --> 08:40.790
as well.

109
08:40.790 --> 08:47.540
Now it just can't get close enough because of the speed and the rotation to make it within that

110
08:47.540 --> 08:49.840
0.5 distance of that waypoint

111
08:49.850 --> 08:52.330
and therefore it's just going to be stuck.

112
08:52.340 --> 08:58.880
So it's the speed, the rotational speed and the accuracy that ALL have an effect on whether the tank will

113
08:59.000 --> 09:02.310
reach that particular waypoint or not.

114
09:02.330 --> 09:09.610
So always consider those and check out what's going on as far as the behavior is concerned.