WEBVTT

0
00:13.370 --> 00:19.220
Hi. Welcome back to this tutorial looking at how to create an NPCs Finite State Machine.

1
00:19.310 --> 00:22.740
So far, we've created the Patrol for our little green tank.

2
00:22.760 --> 00:29.680
Let's now create the Chase, however before we start doing that, I want to create a Base Class for our state

3
00:29.690 --> 00:36.170
machines because we're going to need to have information about where the player is and where the NPC

4
00:36.170 --> 00:43.310
is, to work out distances and angles and things like that and we can do that in each of our state machine

5
00:43.580 --> 00:47.690
behaviors that we've added but we'll have to keep doing that over and over again.

6
00:47.690 --> 00:52.130
So if we create a Base Class, we can put all that information in just once.

7
00:52.130 --> 01:00.440
So let's go into the Assets and our project. We'll create another C# script - let's call that

8
01:00.470 --> 01:02.070
NPCBaseFSM.

9
01:02.330 --> 01:03.150
Ok.

10
01:05.990 --> 01:06.480
Now

11
01:06.530 --> 01:09.880
open that up and in it we'll add this code.

12
01:10.130 --> 01:16.740
So notice it's not a MonoBehaviour, it's a StateMachineBehaviour and it's NPCBaseFSM

13
01:16.760 --> 01:27.650
Inside of here, I'm going to set up a link to our NPC and also one for our opponent and then I'm

14
01:27.710 --> 01:35.330
also putting in some properties for speed, rotational speed and accuracy as well so that we can change

15
01:35.360 --> 01:41.510
those as we like, rather than having them hardcoded back in the previous code.

16
01:41.510 --> 01:47.380
Now the function that we're really interested in overriding and we're using is OnStateEnter.

17
01:47.450 --> 01:55.160
So every time we enter into a state, we want to grab hold of the NPC and the opponent

18
01:55.160 --> 02:03.250
and you'll notice the opponent here is getting a component that's attached to the NPC itself called TankAI

19
02:03.590 --> 02:10.490
and we will write this bit in a moment but this is getting hold of the player and we'll have the tank

20
02:10.490 --> 02:16.130
character know about the actual player, so that it can turn and shoot and move towards it and that type

21
02:16.130 --> 02:16.660
of thing.

22
02:16.760 --> 02:23.330
So this is our Base Class and because we don't have this TankAI script written yet, you're going to

23
02:23.330 --> 02:27.220
get an error saying that it can't find it when you save it.

24
02:27.230 --> 02:30.440
So let's write that TankAI script now.

25
02:30.830 --> 02:37.700
So back in our project, let's create another C# file and call it TankAI and open that up

26
02:37.850 --> 02:40.820
and in this code we will add this.

27
02:40.820 --> 02:47.370
So the beginnings of this TankAI script is that it has a link to the player

28
02:47.480 --> 02:54.620
and then we have a public method call 'GetPlayer', which you saw called before, from our previous code and it's

29
02:54.620 --> 02:56.590
going to return the 'player'.

30
02:56.720 --> 03:02.520
So we can use that to work out the distance between the tank and the player a little later on.

31
03:02.630 --> 03:03.800
So save that,

32
03:03.850 --> 03:11.730
go back into Unity, what you want to do with that code is drag-and-drop it onto the Tank. Select the Tank in

33
03:11.730 --> 03:15.030
the Hierarchy and you'll see the script attached.

34
03:15.030 --> 03:20.280
Now it's got the Player space here, the exposed variable that we created.

35
03:20.280 --> 03:27.750
Drag your Player from the Hierarchy into that space, so now that it knows and it's linked to it, it will

36
03:27.750 --> 03:29.990
be able to work out its distance.

37
03:30.030 --> 03:36.870
Now with that new Base Class, we need to fix our Patrol, so open up your Patrol and instead of inheriting

38
03:36.870 --> 03:44.230
from the StateMachineBehaviour, we're going to inherit from NPCBaseFSM

39
03:44.520 --> 03:50.810
and that's going to include our NPC, so we don't need that...we can remove it from inside of here.

40
03:51.030 --> 03:57.860
We can also remove the setup from in here because it already is going to happen in our OnStateEnter

41
03:57.870 --> 03:59.870
that's back in our Base Class

42
03:59.880 --> 04:03.320
but to ensure that runs, we need to call it.

43
04:03.420 --> 04:10.890
So let's add a line of code and to get back to our base classes OnStateEnter, we call base.OnStateEnter

44
04:10.890 --> 04:15.930
and pass through the same parameters that are coming through to here and that will set up our

45
04:16.030 --> 04:17.520
NPC.

46
04:17.580 --> 04:24.580
Now the NPC is used in our OnStateUpdate and it's already in there.

47
04:24.660 --> 04:27.240
The things that we do need to change in here,

48
04:27.270 --> 04:31.930
remember we added in accuracy, speed and rotational speed?

49
04:32.130 --> 04:41.230
So in the rotation here, we can put our rotSpeed in there (which is coming from this class, remember

50
04:41.230 --> 04:41.990
at the top.

51
04:42.040 --> 04:47.520
We've got speed, rotSpeed and accuracy) and this here's going to be speed

52
04:49.920 --> 04:56.440
and then our accuracy is used up here to determine whether we need to go to the next waypoint, so we'll change

53
04:56.440 --> 04:57.750
that to accuracy.

54
04:58.670 --> 05:04.020
Now if you use those values for anything else in your code, then you can just come back to here and

55
05:04.020 --> 05:07.960
change them on there for your NPC.

56
05:07.980 --> 05:12.550
At this point it's probably worth saving everything and just testing that it still works, so

57
05:12.600 --> 05:17.370
let's just press Play and the Patrol code is still working.

58
05:17.370 --> 05:23.160
All right, so let's move on to getting our Chase state working. In your Animator, create a new state.

59
05:23.160 --> 05:30.060
So, 'right-click > Create State > Empty' and we will call that 'Chase', so go over to the Inspector and give

60
05:30.060 --> 05:40.280
it a new name. Now 'Chase' will get transitioned-to from Patrol, so right-click on Patrol and go

61
05:40.270 --> 05:41.510
'Make Transition'.

62
05:41.510 --> 05:46.520
You'll then get a little arrow that you can click on something else with. You want to click it on to

63
05:46.550 --> 05:48.100
'Chase'.

64
05:48.350 --> 05:50.130
This is your transition

65
05:50.300 --> 05:57.790
and if you select it, you'll see over in the Inspector, a place where we can add our conditions.

66
05:58.220 --> 06:05.930
The other thing to take note of in here also, is this 'Has Exit Time'. The Has Exit Time will wait until

67
06:05.930 --> 06:06.980
patrol has finished

68
06:07.010 --> 06:12.040
whatever it's doing in its Update in order to transition over to Chase.

69
06:12.040 --> 06:18.050
Now, if you just want it to completely interrupt it, then get rid of it, untick it and then it won't

70
06:18.380 --> 06:19.100
wait for that.

71
06:19.100 --> 06:25.490
So if you find you've got lags between transitioning, it'll be because you've still got this Has Exit Time

72
06:25.490 --> 06:30.530
ticked on all of your transitions and it's up to you whether you want it there or not.

73
06:30.530 --> 06:38.570
Now the condition for going from Patrol-to-Chase, let's say it's going to be when the player gets within

74
06:38.570 --> 06:41.750
a distance of 20 units.

75
06:41.750 --> 06:46.660
So we need a parameter on here for 'distance'.

76
06:46.850 --> 06:54.320
So over in the Animator, on the side, there's a Layers and there's a Parameters tab. You want to click Parameters

77
06:54.890 --> 06:59.510
and then there's a little + sign, click that...we're going to add a Float.

78
06:59.540 --> 07:04.430
So add a float and we'll call it 'distance'.

79
07:04.440 --> 07:10.140
Initially we want the NPC to be a long way away from the player.

80
07:10.230 --> 07:15.750
We don't want this to trigger immediately, so if 'distance' is set to 0, this will trigger immediately.

81
07:15.750 --> 07:21.230
Let's set it to something ridiculous like 100 which is totally outside the realm

82
07:21.390 --> 07:30.320
and then what we do here on the transition is click on it. Over in the Inspector, in Conditions, hit the +

83
07:30.740 --> 07:34.460
and it'll immediately come up with distance because that's the only thing it knows about

84
07:34.790 --> 07:43.350
and then 'Greater' than 0. We're saying that this condition is 'distance is Less than 20'.

85
07:43.430 --> 07:46.090
So we'll put a 20 in there.

86
07:46.190 --> 07:54.350
So when this value here gets to less than 20, the transition will happen and the Chase state will start.

87
07:54.540 --> 07:58.980
The next thing we need to do is update what this distance here is,

88
07:58.980 --> 08:02.790
otherwise it will never get down to below 20

89
08:02.940 --> 08:11.010
and the distance will be based on the distance between us (our player) and our NPC.

90
08:11.010 --> 08:18.060
Now you could calculate the distance between the player and the NPC inside of your state machine but

91
08:18.090 --> 08:25.140
you want to be doing that over and over again and it's a condition that is used throughout your entire

92
08:25.140 --> 08:29.340
state machine and therefore it makes sense to do it externally to this

93
08:29.370 --> 08:32.080
and the code for that will go on the TankAI

94
08:32.130 --> 08:40.410
instead. Doing it this way will also show you how to inject a value from your MonoBehaviour into

95
08:40.410 --> 08:44.210
the animation system so that you can change that condition.

96
08:44.310 --> 08:51.390
First of all, in the TankAI script, you're going to need to grab hold of the animator, so we'll create

97
08:51.450 --> 08:52.190
an animator 'anim'

98
08:52.210 --> 08:59.430
at the top here and then in the Start function, we'll set that up so that it points to the Animator

99
08:59.430 --> 09:02.040
that's attached to your game object (or the tank

100
09:02.040 --> 09:10.590
in this case) so it will be anim = GetComponent for the Animator...like that.

101
09:10.590 --> 09:14.720
So now you have your Animator set up.

102
09:14.850 --> 09:21.930
Now the distance is calculated between this object (the tank) and the player that you've got here

103
09:21.930 --> 09:29.220
and so you can inject that into that 'distance' value in the Update, so you can constantly be working out

104
09:29.220 --> 09:32.660
the distance and sending it through to the state machine.

105
09:32.670 --> 09:33.500
So in Update,

106
09:33.510 --> 09:39.720
let's add this piece of code...and what it's doing is it's taking your Animator and calling the SetFloat

107
09:39.840 --> 09:41.030
method on it.

108
09:41.040 --> 09:44.560
What that will do is set the distance value

109
09:44.660 --> 09:50.760
(make sure it's in quotes here and it's spelled exactly the same as you have your parameter up there because it's referring

110
09:50.760 --> 09:55.860
to that) and then whatever you put in the next part of the method will be the value that it's going to

111
09:55.860 --> 09:57.060
change it to.

112
09:57.100 --> 10:03.870
I'm using a Vector3.Distance to calculate the distance between the position of our tank and

113
10:03.870 --> 10:05.970
the position of the player.

114
10:05.970 --> 10:07.940
So let's have a look how this works.

115
10:07.980 --> 10:12.080
Save that and switch back into Unity and press Play.

116
10:12.090 --> 10:18.450
Now if you look over here at the distance (as this is playing) you'll see it changing and that's because

117
10:18.450 --> 10:20.360
the tank is moving.

118
10:20.370 --> 10:26.250
You'll also have noticed that there was a transition when this distance got below 20.

119
10:26.250 --> 10:29.730
So let's just play that again and just watch it.

120
10:29.730 --> 10:36.930
So I'm going to move the player this time and as I get within 20, the transition down to Chase happens

121
10:36.960 --> 10:41.360
and then it's stuck in this Chase because it's got nowhere else to go.

122
10:41.370 --> 10:46.800
There's also no code added as a behavior there, so the tank is not going to do anything at all but we know

123
10:46.800 --> 10:51.070
that it's been triggered because we saw it here and we saw the tank stop.

124
10:51.270 --> 10:58.340
So time to add in our Chase behavior code, so select Chase in your Animator and go over to the Inspector...

125
10:58.590 --> 11:00.080
click on [Add Behaviour]...

126
11:00.240 --> 11:07.770
this time it's going to be called 'Chase', [Create and Add] and you'll get Chase appear down the bottom and

127
11:07.770 --> 11:15.210
let's open that up and to Chase, we're going to open up the same lot of methods that we had before in

128
11:15.210 --> 11:19.410
the Patrol and add some code into the Update.

129
11:19.430 --> 11:21.050
Chase now looks like this

130
11:21.090 --> 11:22.370
(and let me just bring this out).

131
11:22.410 --> 11:23.960
You're only missing on the edge

132
11:23.970 --> 11:31.130
the squiggly brackets but just so you can see the code completely. Chase will inherit from NPCBaseFSM

133
11:31.290 --> 11:38.280
to pick up the details about the NPC and then OnStateEnter will call our base.OnStateEnter

134
11:38.280 --> 11:44.400
again, in order to just set that up again because remember, it's not going to get it passed through from

135
11:44.400 --> 11:48.870
Patrol because as far as they're concerned, they don't even know about each other,

136
11:49.200 --> 11:53.670
so it's got no way of getting that unless it gets it from the base.

137
11:53.670 --> 11:59.490
Now we've got our OnStateUpdate that's going to do all the work here and all this is doing is just

138
11:59.880 --> 12:08.900
setting up the Chase code, which will have the tank turn towards the player and follow the player's tank.

139
12:08.910 --> 12:15.230
So first of all you need to work out what the direction is between the opponent (which is the player) and

140
12:15.300 --> 12:16.860
the NPC itself.

141
12:16.890 --> 12:18.270
That's the direction.

142
12:18.270 --> 12:26.520
Then we slerp in that direction using our rotSpeed that we've got inside of our Base Class and then

143
12:26.520 --> 12:33.910
we run a Translate on here based on the speed along the forward vector of the tank.

144
12:33.920 --> 12:40.410
This movement code here is pretty much the same as what we've got for turning towards waypoints and

145
12:40.410 --> 12:44.110
pushing the NPC in that direction.

146
12:44.190 --> 12:44.540
All right.

147
12:44.550 --> 12:48.470
Save all of that and then switch back to Unity and we can try this out.

148
12:48.480 --> 12:53.280
What should happen is we will 'patrol' until we get to a distance of less than 20

149
12:53.400 --> 13:00.360
and then the 'chase' will start and in the chase, the tank will just keep following our player, so let's

150
13:00.360 --> 13:02.830
get our player over there

151
13:04.770 --> 13:06.320
and we're now in chase mode

152
13:06.330 --> 13:13.470
and if I just keep moving my player, you'll see that it'll follow it and it's going to follow it forever because

153
13:13.470 --> 13:18.600
there's no way for it to transition back to Patrol

154
13:18.600 --> 13:22.030
but that's easily fixed because all the code is in place for that.

155
13:22.170 --> 13:28.500
So let's just stop playing and to transition back from Chase to Patrol, when the distance becomes

156
13:28.500 --> 13:32.190
greater than 20, then we want the tank just to go back to Patrol.

157
13:32.190 --> 13:40.710
So right-click on Chase, Make Transition and drop the arrow on to Patrol, select the arrow and then

158
13:40.710 --> 13:42.560
let's go over to the Inspector,

159
13:42.590 --> 13:51.480
untick 'Has Exit Time' add the condition of 'distance greater than 20'.

160
13:51.500 --> 13:54.510
So we now have...less than 20 we'll

161
13:54.560 --> 13:58.330
go from Patrol to Chase and then greater than 20,

162
13:58.340 --> 14:04.290
we'll go back to patrolling and because these state behaviors are set up in here,

163
14:04.310 --> 14:06.370
that's as simple as it gets.

164
14:06.380 --> 14:09.580
We're just injecting the distance constantly from our TankAI

165
14:09.610 --> 14:16.640
and the state machine's doing the rest of the work.  So let's press Play and I'll move my tank over.

166
14:16.710 --> 14:23.560
So we're now in Chase mode...and the tank will follow and turn towards my player...

167
14:23.870 --> 14:31.870
but then if I drive away...and get beyond 20, patrol will set back in and the tank will go back to patrolling

168
14:32.640 --> 14:34.730
and remember the OnStateEnter for Patrol

169
14:34.740 --> 14:40.560
makes the tank go back to the first waypoint every time in its set-up.

170
14:40.950 --> 14:45.340
OK so that's setting up the chase. In the next tutorial,

171
14:45.360 --> 14:48.140
we'll look at adding in 'Attack'.