WEBVTT

0
00:11.360 --> 00:16.490
In the Line Of Sight workshop, we set up a variable called 'state' to keep track of the activity the NPC

1
00:16.490 --> 00:22.580
was currently performing. A.I. methods using states are very common in NPC programming and no more focused

2
00:22.580 --> 00:25.970
upon, than in the case of Finite State Machines.

3
00:25.970 --> 00:30.770
In this lecture, we're going to build a Finite State Machine from scratch and use it to control a tank

4
00:30.800 --> 00:34.330
that will be performing 'guarding' & 'chasing' functions.

5
00:36.170 --> 00:42.120
A Finite State Machine is a conceptual machine that can be in exactly 1 of any given number of finite

6
00:42.120 --> 00:48.570
states any time. A Finite State Machine is used in the field of Artificial Intelligence to define

7
00:48.630 --> 00:55.140
(among other things) the behaviors of computer-controlled characters. It is represented as a graph with nodes that

8
00:55.140 --> 01:01.780
represent the states and transitions, that define the path from one node to the next. Once a state has

9
01:01.780 --> 01:02.550
been entered,

10
01:02.560 --> 01:07.040
the NPC stays in that state until a condition on the transition is met.

11
01:07.060 --> 01:10.990
Here we have a Finite State Machine for simple guarding behavior.

12
01:11.110 --> 01:16.480
If the NPC is in the Patrol state, it will continue performing the associated behaviors until a condition

13
01:16.540 --> 01:19.960
is met on any of the transitions exiting the state.

14
01:19.990 --> 01:25.290
In this case, it is waiting until the player is a distance less than 20 away.

15
01:25.300 --> 01:32.780
Once that condition is met, the NPC transitions into the Chase state. If you've been using Unity and think

16
01:32.780 --> 01:38.410
the Finite State Machine concept looks familiar, it's because it is! The mecanim system uses Finite

17
01:38.410 --> 01:44.440
State Machines to control animation behaviors and it's exactly what we are about to use to define the

18
01:44.440 --> 01:47.890
behavior of the little green tank. To follow along

19
01:47.890 --> 01:52.540
as I build this F.S.M., download the starter file that comes with this lecture.

20
01:52.720 --> 01:58.480
This uses a lot of the assets from the tanks tutorial that Unity released a couple of years ago and

21
01:58.480 --> 02:02.260
I've repurposed them to use them in this situation.

22
02:02.260 --> 02:08.350
When you first open the scene that's called Tanks, you'll see this. So we've got a little green tank

23
02:08.350 --> 02:14.230
which will be our NPC and we have a tank that we can drive - it already has a script on it. If you press

24
02:14.230 --> 02:19.270
Play, you'll be able to use the arrow keys to drive it around...like that.

25
02:20.100 --> 02:24.710
The first thing we need to do for our green tank is to create an animation controller.

26
02:24.870 --> 02:32.460
So go into the project and click on Create and you're looking for Animator Controller, which is off my

27
02:32.460 --> 02:33.490
list here.

28
02:33.890 --> 02:40.410
Let me bring this up for you...Create > Animator Controller.

29
02:40.710 --> 02:44.910
If you've used animation before in Unity, you'll be familiar with this.

30
02:44.910 --> 02:54.000
We're going to call this our NPCFSM, like that. To start creating the states,

31
02:54.060 --> 02:59.530
double-click on the NPCFSM and that will open up the Animator window.

32
02:59.730 --> 03:06.420
The first state that we'll create is Patrol. Right-click in the Animator window, go 'Create State > Empty' then

33
03:06.480 --> 03:11.100
select that state and over in the Inspector, you can change its name.

34
03:11.460 --> 03:19.020
Instead of assigning this state with animations we will assign it with behaviors and over in the Inspector,

35
03:19.020 --> 03:21.810
you'll see that there's an 'Add Behaviour' button.

36
03:21.810 --> 03:30.730
Select that and we'll create a new script. We'll call it 'Patrol' and 'Create and Add' that. That will create

37
03:30.730 --> 03:38.380
a C# file called Patrol in your Assets folder that is now associated with this Patrol state

38
03:38.560 --> 03:45.760
and if you double-click and open this up, you'll find a whole bunch of commented-out code which is triggered

39
03:45.970 --> 03:53.180
by that state. Any state in a state machine has 3 processes that it runs at different times.

40
03:53.290 --> 04:00.640
It has an Enter, it has an Update and it has an Exit. Enter runs as soon as the state is transitioned to.

41
04:00.700 --> 04:08.320
The state then enters the endless loop of the Update. Update continues to run until the transition

42
04:08.410 --> 04:12.540
out of the state is triggered. At this point, before leaving the state,

43
04:12.550 --> 04:19.980
the Exit method is run. Back in our state machine behavior script, we'll find each of these methods included.

44
04:19.990 --> 04:21.980
The first is an Enter

45
04:22.030 --> 04:24.520
(let's take away the comments for that).

46
04:24.520 --> 04:28.650
The next is the Update (remove the comments for that as well)

47
04:29.020 --> 04:31.190
and lastly there's an Exit

48
04:31.420 --> 04:34.010
(let's also get rid of those comments).

49
04:34.210 --> 04:39.630
The other methods ('OnStateMove' and 'OnStateIK') are used by the animation system.

50
04:39.640 --> 04:44.620
We're not doing animation in this case, so let's just remove those.

51
04:44.710 --> 04:50.510
We'll now build a really simple 'waypoint following' system into this code. At the top,

52
04:50.510 --> 04:55.000
I'm going to define 3 values like this.

53
04:55.030 --> 05:03.760
So the first one is 'NPC' and this is to grab hold of the NPC that is using this code via their Animator

54
05:04.270 --> 05:05.710
and we'll get to that in a moment.

55
05:05.710 --> 05:11.860
Next we need an array of all the waypoints, so that we can loop through those and move the NPC around

56
05:11.860 --> 05:17.290
and then we need to keep track of which waypoint we're currently going towards, which will be in this

57
05:17.380 --> 05:20.150
int called currentWP.

58
05:20.170 --> 05:24.310
Now to get hold of the waypoints, because this is a scriptable object

59
05:24.310 --> 05:32.140
there's no way for the Inspector to send values through to this and therefore we need to grab them at

60
05:32.170 --> 05:33.380
runtime.

61
05:33.610 --> 05:38.680
We can add an Awake method (which I'll just do up here) to this

62
05:38.680 --> 05:45.690
and what it's going to do is set waypoints to find all of the GameObjects with the tag of 'waypoint'.

63
05:46.690 --> 05:52.180
Once you've got that in there, if you just switch back to Unity (I'll just save it) and have a

64
05:52.180 --> 05:57.440
look at the waypoints that I've added and you'll see that they're tagged with 'waypoint'.

65
05:57.460 --> 06:01.660
So that's how the script is going to find them. OnStateEnter

66
06:01.710 --> 06:08.680
is going to run every time this state gets transitioned to, no matter what the NPC happens to

67
06:08.680 --> 06:16.740
be doing. So you need to consider what you need in here that's going to set up the NPC for patrolling,

68
06:16.810 --> 06:17.930
in this case.

69
06:18.070 --> 06:21.510
Now what I want to do is to add these.

70
06:21.730 --> 06:30.110
First of all, I'm going to grab hold of the NPC (which is our value that we've got defined at the top).

71
06:30.130 --> 06:36.760
Now we can get the NPC running this script using the animator object that's being passed through here

72
06:37.330 --> 06:43.090
and I'm only setting this up just to save myself a bit of typing, as far as having to type this in every

73
06:43.090 --> 06:46.940
time I wanted to get the transform or position of the NPC.

74
06:46.960 --> 06:48.730
So that's just grabbing hold of that.

75
06:48.760 --> 06:56.750
Now every time we come into Patrol, I'm going to tell the NPC that it needs to go to waypoint 0.

76
06:56.980 --> 07:04.570
So if it gets interrupted in its quest for patrolling (by having to chase somebody) then it will always

77
07:04.600 --> 07:09.970
go back and start at position 0 and re-count again from there.

78
07:10.000 --> 07:17.770
So that's OnStateEnter. OnStateUpdate is going to run over and over again, while the NPC is in this

79
07:17.770 --> 07:18.620
state.

80
07:18.640 --> 07:24.940
So this is where we're going to put the code for patrolling and moving around those waypoints.

81
07:24.940 --> 07:31.120
What this is doing is, first of all, it's asking if there's any waypoints at all (so if the waypoint array

82
07:31.120 --> 07:37.210
is 0 in length, then 'return' because there's no point trying to move around non-existent

83
07:37.210 --> 07:38.030
waypoints).

84
07:38.260 --> 07:46.240
Next, it's doing a distance calculation and this distance calculation works out where the NPC is in relation

85
07:46.390 --> 07:54.050
to the current waypoint it's heading towards. If it's reached the current waypoint within an accuracy

86
07:54.070 --> 08:01.390
in this case, of 3, then it will go and get a new waypoint and so it will update waypoint by 1. Starts at

87
08:01.390 --> 08:01.810
0,

88
08:01.810 --> 08:03.820
so now it's going to go up to 1.

89
08:03.860 --> 08:08.770
It then will ask if the waypoint is greater than or equal to the length of the array,

90
08:08.860 --> 08:10.930
it will set the current waypoint back to 0.

91
08:10.930 --> 08:17.920
So this will cycle around (or count up to the end and then cycle back around) and will create a circuit

92
08:18.040 --> 08:19.180
of waypoints.

93
08:19.300 --> 08:24.280
After that is some code that will move the tank forward, as well as make it face in the direction of

94
08:24.280 --> 08:26.470
the waypoint that it's traveling towards.

95
08:26.500 --> 08:34.540
First we calculate the direction in which we want the tank to travel and then using that direction, we

96
08:34.600 --> 08:36.040
apply a slerp.

97
08:36.040 --> 08:44.860
So Quaternion.Slerp is going to slowly turn the NPC towards the waypoint.

98
08:44.860 --> 08:50.280
Now you can modify how fast this rotation happens with this quantifier here.

99
08:50.290 --> 08:55.330
So at the moment, the speed of the rotation is set to 1.

100
08:55.330 --> 09:03.520
After that, we then do a Translate and we push the NPC along its z-axis (which is its forward-facing axis).

101
09:03.520 --> 09:09.460
So the rotation is doing all the work as far as getting orientated with the waypoint and this

102
09:09.460 --> 09:11.350
Translate is just pushing it forward

103
09:11.560 --> 09:17.740
and the speed, in this case, for our forward direction is a value of 2 and that's where you can change

104
09:17.740 --> 09:21.670
it if you want to be moving faster. For OnStateExit,

105
09:21.670 --> 09:26.400
there isn't anything we need at this point so we'll just leave that empty.

106
09:26.650 --> 09:35.110
So now let's save our Patrol and we'll flick back into Unity. What we'll do now is on our green

107
09:35.110 --> 09:35.490
tank,

108
09:35.500 --> 09:41.720
we're going to add this animation (which is our finite state machine instead of an animation,

109
09:41.770 --> 09:49.730
but the process is the same). So we will add a component ...we'll add an Animator and then into the Controller

110
09:49.730 --> 09:53.610
part of the Animator, we want to drag-and-drop our

111
09:53.660 --> 10:02.850
NPCFSM that we just created. Of course, there's no Avatar or Root Motion to apply because we're

112
10:02.850 --> 10:10.730
just using the behavior system within this Finite State Machine to drive the movement of our tank.

113
10:10.740 --> 10:13.410
Now, with all that set up, we can press Play.

114
10:13.590 --> 10:19.380
You can see that behavior working in the Animator window, if you select the current state that's

115
10:19.380 --> 10:20.080
going.

116
10:20.100 --> 10:24.580
If you can't see that, select the tank in the Hierarchy as well.

117
10:24.960 --> 10:30.740
What this is showing you is that the Update method is running over and over again.

118
10:30.930 --> 10:37.200
Now there's no transitions out of here to trigger anything else to happen, except that this little green

119
10:37.200 --> 10:43.500
tank is just going to go around the waypoints in the order that they end up in that waypoint array.

120
10:43.740 --> 10:48.720
In the next tutorial, we'll start adding more states to our Finite State Machine so that the tank can

121
10:48.720 --> 10:51.330
start shooting and chasing the player around.