WEBVTT

0
00:09.030 --> 00:13.530
Before you go ahead and build yourself a full-blown waypoint system, you should probably know it's

1
00:13.590 --> 00:15.570
already available in Unity.

2
00:15.690 --> 00:19.860
So we've built our own little waypoint circuit

3
00:19.980 --> 00:24.560
but now I'm going to show you what Unity has available in it for you.

4
00:24.750 --> 00:31.530
So if you grab the code that I've included as a resource with this lecture, inside you'll find a

5
00:31.530 --> 00:38.970
WaypointCircuit and a WaypointProgressTracker script and drag-and-drop those into your project.

6
00:38.970 --> 00:47.250
These two files I have gotten from a download and import of the Utility package.

7
00:47.310 --> 00:53.430
So it's under Assets > Import Package > Utility. That will bring in a folder called Standard Assets

8
00:53.610 --> 01:01.020
and in that, somewhere, you will find these 2 scripts but I've included them so that you can quickly

9
01:01.050 --> 01:05.240
add them and I'll show you the difference that you get using these.

10
01:05.280 --> 01:13.110
First of all, there's a waypoint circuit and this will set up what we've achieved with an array and

11
01:13.110 --> 01:18.420
these balls, but you'll still need to have the balls in there because they are waypoints.

12
01:18.450 --> 01:29.060
So first, in the Hierarchy, Create Empty and we will call this our WPCircuit. To WPCircuit,

13
01:29.280 --> 01:38.130
drag-and-drop the WaypointCircuit script, then get all of the spheres that you're using for your waypoints,

14
01:38.270 --> 01:46.370
collect them up and drag-and-drop them so that they're 'child objects' of WPCircuit.

15
01:46.370 --> 01:52.640
Now select the parent (the WPCircuit). Over in the Inspector, where you've added that code, you'll see a button

16
01:52.640 --> 02:00.020
that says [Assign using all child objects]. If you click on that, it will create an automatic waypoint

17
02:00.080 --> 02:02.750
out of all of your objects.

18
02:02.750 --> 02:09.920
It will also link them up with a nice curve (which we can't see because my spheres are slightly under

19
02:09.920 --> 02:10.650
the ground).

20
02:10.790 --> 02:18.830
If I just lift those up, you'll see this circuit being drawn in the scene and you can see very easily

21
02:18.830 --> 02:24.620
from that that you have a circuit and none of the waypoints in the wrong order for being

22
02:24.620 --> 02:26.100
a complete circuit.

23
02:26.210 --> 02:32.270
If you want to change the order they appear in, over in this Waypoint Circuit component that you've

24
02:32.300 --> 02:33.700
added on to there,

25
02:33.800 --> 02:38.310
let me grab the second one and I'll move it up and you'll see the result.

26
02:38.330 --> 02:45.680
Now that Sphere(1) is the first one and Sphere(0) is the second one, you're going to get this crossover

27
02:45.740 --> 02:47.800
and that might be what you want.

28
02:47.990 --> 02:48.990
That's fine.

29
02:49.010 --> 02:56.030
You can also grab hold of your waypoints and move them manually around as well, to change the particular

30
02:56.060 --> 02:57.990
order of your circuit.

31
02:58.460 --> 03:02.440
So they're the 2 things you should be aware of in setting up the circuit.

32
03:02.450 --> 03:05.700
I'll just leave it crossed over there for a little bit of interest.

33
03:05.840 --> 03:11.030
At this point, if you want to keep using your WaypointFollow script that we've already written on your

34
03:11.030 --> 03:19.750
tank, then you need to update it to use the new waypoint circuit, instead of the individual spheres

35
03:19.850 --> 03:22.820
in the game object array that we had.

36
03:22.820 --> 03:31.730
So let's take a look how we would update the script. Back in the WaypointFollow, instead of this

37
03:31.730 --> 03:33.560
public GameObject array at the top,

38
03:33.560 --> 03:37.350
we now grab hold of the circuit itself.

39
03:37.400 --> 03:46.220
Now that circuit is a WaypointCircuit type and it's in the namespace UnityStandardAssets.Utility because

40
03:46.220 --> 03:49.510
remember I said that it does come in the Utility import

41
03:49.520 --> 03:54.770
if you bring it down from there, so make sure that you declare it like that. It's going to be public, so that we

42
03:54.770 --> 03:59.400
can tell it where the circuit is and drag and drop it in the Inspector.

43
03:59.480 --> 04:03.110
The rest of the code is ok up here. In the Start,

44
04:03.140 --> 04:08.760
you won't have to get your waypoints with a FindGameObjectsWithTag any more.

45
04:08.870 --> 04:16.920
So we can comment that out. Down in the LateUpdate where we're getting each waypoint and

46
04:16.920 --> 04:19.210
then turning towards it and moving towards it,

47
04:19.260 --> 04:26.550
we just have to change our reference to what we're getting. So instead of the game object indexed by current

48
04:26.670 --> 04:34.340
waypoint, we're getting the current waypoint from the list of waypoints that comes from the circuit.

49
04:34.350 --> 04:38.370
So the first thing we do is check the length of the circuit.

50
04:38.370 --> 04:44.100
So instead of waypoints.length that we had before, it's now

51
04:44.100 --> 04:44.780
circuit.Waypoints.length

52
04:44.820 --> 04:47.820
circuit.Waypoints.length

53
04:48.040 --> 04:57.120
and when we get our goal, we're getting the circuit.waypoints[currentWP].position.x so

54
04:57.130 --> 04:59.250
the transform has gone in here

55
04:59.380 --> 05:04.920
because these waypoints have the positions straight out from there, you don't need the transform.

56
05:05.020 --> 05:11.140
So just be sure that you haven't got that. Again, if you're in 3D space and you want to use the z

57
05:11.140 --> 05:13.000
for the waypoint as well,

58
05:13.090 --> 05:17.430
go ahead...put it in here, instead of the height of your character.

59
05:17.680 --> 05:25.270
The other thing you need to change is just down inside the direction.magnitude part where we're

60
05:25.660 --> 05:31.090
testing if we've run out of waypoints and we're going back to the beginning again, we need to get the

61
05:31.190 --> 05:35.830
circuit.Waypoints.length not waypoints.length that we had before.

62
05:36.160 --> 05:39.950
So save that and switch back into Unity and press Play

63
05:42.170 --> 05:48.320
and you'll be able to watch your tank move around your circuit of waypoints. So it's the same waypoints

64
05:48.320 --> 05:55.480
we had before, but now we're just organizing them using Unity's WaypointCircuit code that it brings

65
05:55.580 --> 06:01.790
and it's very useful because first of all, it gives you the visualization of what your circuit is going

66
06:01.790 --> 06:03.940
to look like in the scene

67
06:04.160 --> 06:09.770
and it allows you to reorganize these waypoints. Using this method,

68
06:09.770 --> 06:13.970
you'll notice that the tank does not stick to the yellow line. The yellow line

69
06:13.970 --> 06:20.570
in this case, is just a guide for where the circuit is and which waypoint is connected to which other

70
06:20.570 --> 06:21.860
waypoint.

71
06:21.860 --> 06:25.380
Now we're going to bring the WaypointProgressTracker into play.

72
06:25.400 --> 06:32.570
So this is the other Unity created script that comes in Utilities. It's going to be attached to our tank.

73
06:33.040 --> 06:39.920
Select the tank in the Hierarchy and remove the WaypointFollow script that we created. Then instead,

74
06:40.380 --> 06:44.110
in the Inspector, you want to drag-and-drop the WaypointProgressTracker.

75
06:44.120 --> 06:48.680
Now this progress tracker you'll see also requires a circuit of waypoints and you can give it the

76
06:48.680 --> 06:51.620
circuit that we created just before.

77
06:51.620 --> 06:56.070
The other thing it also has down the bottom is a Target.

78
06:56.090 --> 07:01.260
The Target is going to be an object that is a child of the tank.

79
07:01.280 --> 07:04.360
So select the tank, right-click

80
07:04.430 --> 07:09.070
(this is in the Hierarchy) 3D Object > Cube.

81
07:09.170 --> 07:14.920
Grab that cube and move it away from the tank a little bit - you can see it there.

82
07:14.930 --> 07:20.380
The Cube is going to become a goal for our tank to travel towards

83
07:20.540 --> 07:26.060
and the cube will stick to the circuit and be pushed around the circuit

84
07:26.060 --> 07:31.490
by the WaypointProgressTracker code and the tank is going to follow it.

85
07:31.490 --> 07:36.670
So the cube itself doesn't even need to be visible and later, when you put this into production,

86
07:36.680 --> 07:37.840
you wouldn't have it visible.

87
07:37.850 --> 07:42.680
First of all, just turn off your box collider because you don't need any strange physics occurring

88
07:42.710 --> 07:43.980
in this system.

89
07:44.020 --> 07:51.970
Now select the parent tank and drag-and-drop your little cube into the target.

90
07:51.960 --> 07:55.510
Right, at this point, let's press Play and see what happens.

91
07:56.960 --> 07:57.200
All right.

92
07:57.200 --> 08:03.270
So you can see that you've got the cube and it's being moved. It's sitting on the circuit.

93
08:03.300 --> 08:07.580
There's this green line going between the tank and the cube

94
08:07.790 --> 08:12.560
and there's also this ball here as well on the circuit.

95
08:12.560 --> 08:19.070
Now all these values are controlled in the WaypointProgressTracker script that's attached to the tank.

96
08:19.070 --> 08:27.890
Basically, what they're doing is creating a little rubber band between our cube and our tank, such that

97
08:28.010 --> 08:34.850
the cube can't get too far away from the tank whilst it's staying on the circuit.

98
08:34.850 --> 08:38.760
If I move my tank closer to the cube

99
08:39.970 --> 08:42.640
and start to come up behind it,

100
08:44.150 --> 08:50.290
you'll see that the cube makes its way along the circuit.

101
08:50.300 --> 08:52.100
So that's the progress tracker

102
08:52.100 --> 08:55.550
moving the cube for us, automatically.

103
08:55.640 --> 08:59.930
Now you don't want to play your game by dragging the tank around in the Scene view!

104
09:00.050 --> 09:05.810
So we want to program it to automatically follow the cube and we've already created this code, so if you

105
09:05.810 --> 09:12.530
remember when we had the zombie following the driveable cube around and we created a script called

106
09:12.530 --> 09:14.030
MoveLocal?

107
09:14.030 --> 09:21.290
It's that exact same script that we now need to put on the tank and tell it that our goal is going to

108
09:21.290 --> 09:22.720
be the cube.

109
09:22.730 --> 09:27.540
So I've created this script in a file called FollowGoal.

110
09:27.590 --> 09:34.410
Besides the name change to FollowGoal, there's really only one other line of code that has been modified.

111
09:34.430 --> 09:40.190
We still have our 'public goal' that we're going to pass through here and now instead of it being a driveable cube,

112
09:40.190 --> 09:45.880
it's going to be that cube that is being driven by the circuit.

113
09:46.130 --> 09:49.760
We still have our speed, accuracy and our rotation, as we had before.

114
09:49.760 --> 09:55.880
In fact, we're not even using accuracy in this case so you could take that out. In the LateUpdate

115
09:55.970 --> 10:00.270
is where all the work happens, so we still want to do a lookAtGoal.

116
10:00.350 --> 10:09.170
Then we set our direction,as before. We slerp-rotate towards that direction and then we push our character

117
10:09.170 --> 10:10.150
forward.

118
10:11.000 --> 10:19.220
Notice that the accuracy 'if' check has now gone because we don't want our tank to stop when it gets close

119
10:19.220 --> 10:23.960
to that cube because if it stops, the cube stops and you don't want that, you just want the cube to keep

120
10:23.960 --> 10:27.520
moving around the circuit and the tank to keep following.

121
10:27.530 --> 10:34.970
So save that and go back into Unity, select the tank, grab hold of that new FollowGoal code, drag-and-drop

122
10:34.970 --> 10:40.300
it onto the tank in the Inspector, so that you'll have the WaypointProgressTracker and you'll

123
10:40.310 --> 10:42.540
also have your FollowGoal.

124
10:42.560 --> 10:50.830
For the exposed Goal variable, you want to add your cube object, so drag-and-drop that down into the 'Goal'.

125
10:50.840 --> 10:59.030
Now when you press Play and you'll see I've already aligned my cube ready to go to point 1 and my tank

126
10:59.060 --> 11:00.110
just behind it.

127
11:00.110 --> 11:02.330
Of course you can have these anywhere.

128
11:02.570 --> 11:03.960
Let's press play

129
11:04.770 --> 11:10.520
and now the cube will start calculating its trajectory along the circuit

130
11:10.560 --> 11:13.980
and you can see that the tank is moving.

131
11:13.980 --> 11:19.200
The other thing you'll find is because of the way this tank is following the cube with the speed and

132
11:19.200 --> 11:29.080
the rotation, is that the tank can cut corners...because the tank is trying to go straight towards the

133
11:29.080 --> 11:29.520
cube.

134
11:29.560 --> 11:34.780
It really has no knowledge and doesn't care about your waypoints.

135
11:34.780 --> 11:37.770
So the cube is acting as a little guide.

136
11:37.780 --> 11:45.010
Now if you want to modify how far ahead that cube can get and how fast it goes around the corners, then

137
11:45.010 --> 11:47.530
you can change in the WaypointProgressTracker

138
11:47.530 --> 11:56.350
how far it will 'look ahead' and how far it's speed for 'look ahead' will be as well.

139
11:56.440 --> 12:04.210
You can also modify a point-to-point threshold and this threshold is how close we want the cube itself

140
12:04.210 --> 12:08.480
to get to each of these before it's considered having visited them

141
12:08.480 --> 12:12.550
and that's essentially our accuracy.

142
12:12.550 --> 12:15.700
And so that's the waypoint script that comes with Unity.

143
12:15.700 --> 12:22.370
Feel free to use it both together (the circuit and the tracker) or use one of them with your code,

144
12:22.380 --> 12:23.290
whatever you like.

145
12:23.350 --> 12:30.600
They're all doing the same thing. Our code is really quite simple because we're just pushing an A.I. forward

146
12:30.690 --> 12:32.080
around the circuit

147
12:32.130 --> 12:38.330
and so you might prefer to use that because you've written it and you can modify it to your own understanding.