WEBVTT

0
00:12.520 --> 00:16.960
In this lecture, we're going to implement a GOAP system in Unity.

1
00:16.960 --> 00:19.840
Now you can download the starter file to follow along

2
00:19.840 --> 00:21.740
and it's called GOAP.zip.

3
00:21.820 --> 00:25.950
There's 2 starter files with this lecture...there's the GOAP.zip

4
00:26.170 --> 00:28.310
and there's also a goap-master.zip.

5
00:28.390 --> 00:36.040
The goap-master is the complete version of this GOAP library that I've taken from the URL that you can see

6
00:36.040 --> 00:36.760
on the screen.

7
00:36.760 --> 00:41.230
So it's got other examples and things in it for you to look at later

8
00:41.380 --> 00:47.290
and what I've done with just the GOAP one is I've stripped it back as much as I can and I've also modified

9
00:47.290 --> 00:55.000
it to allow for a Nav Mesh Agent because we'll use a NavMesh to push our agents around on the screen

10
00:55.240 --> 00:57.560
between their different destinations.

11
00:58.330 --> 01:05.500
Now, if you bring the GOAP.zip package into Unity, then you'll find THIS when you open up the

12
01:05.500 --> 01:06.320
village scene.

13
01:06.460 --> 01:09.770
I've got an agent and he's going to be a Baker.

14
01:09.940 --> 01:11.590
This is his Bakery...

15
01:11.590 --> 01:14.100
this is the Windmill supplying the flour...

16
01:14.320 --> 01:22.110
and then over here, we've got some Markets. Now, with each of these buildings I've included a cube

17
01:22.110 --> 01:23.140
(which I've called 'Door')

18
01:23.140 --> 01:31.390
and you can see that in the Hierarchy. The agent needs a location to go to, to perform the task

19
01:31.540 --> 01:35.460
and you could have them go to the actual building to perform the task

20
01:35.470 --> 01:39.380
but I thought it would be nice to have just a little marker where they could go to,

21
01:39.400 --> 01:43.920
so that's why I've added them here and you'll see them in use later.

22
01:44.080 --> 01:48.080
Now this agent is on a plane, which is a NavMesh.

23
01:48.100 --> 01:54.510
If you open up your Navigation tab, you'll see that the NavMesh is already applied.

24
01:54.700 --> 02:02.410
The agent (which is called Baker because we're going to get him to bake some bread) in the Hierarchy

25
02:02.710 --> 02:04.960
has got a few things already set up on it.

26
02:04.960 --> 02:10.870
So, first of all, it has the Nav Mesh Agent on there, which is what we all use to navigate around the

27
02:10.870 --> 02:12.080
scene.

28
02:12.280 --> 02:21.820
You also need to add to any agent (that's a GOAP agent) the 'Goap Agent' script and that Goap Agent script

29
02:22.060 --> 02:24.410
is in the Assets in the GOAP folder.

30
02:24.460 --> 02:30.100
We'll get to that in a moment, that's already written for you. Then the things that you have to write

31
02:30.100 --> 02:36.340
that sit over the top of the GOAP system are the scripts that run the agent itself and we're calling

32
02:36.340 --> 02:44.780
him a Worker and the agent also has an Inventory and I'll come back and look inside of those in a second.

33
02:44.980 --> 02:49.120
Let's go over to the Assets and have a look in the AI folder.

34
02:49.270 --> 02:52.770
You're going to find that there's a Finite State Machine (FSM) folder.

35
02:52.780 --> 02:58.630
This is the little state machine that just drives the agent from one location to the other to perform

36
02:58.690 --> 03:00.010
the tasks.

37
03:00.010 --> 03:04.880
It doesn't use the State Machine System that's built into Unity, it's all done by code.

38
03:04.900 --> 03:10.870
It's such a simple thing that it doesn't really need the Animation System to set up

39
03:10.870 --> 03:17.110
all of the different states and transitions between it because it's essentially only 2 states

40
03:17.110 --> 03:18.540
that you have to deal with.

41
03:18.670 --> 03:24.940
Then there's a GOAP folder. Go into the GOAP folder and this is where you'll find all of the classes

42
03:25.120 --> 03:34.150
that meet the different parts of the GOAP architecture. First there's GOAP agent, which takes care of the basic

43
03:34.300 --> 03:40.000
agent actions and makes sure that it's going to run the Planner, that it's looking for Goals, that it's

44
03:40.000 --> 03:43.510
keeping track of the World State as it changes.

45
03:43.510 --> 03:45.760
We then have a GoapAction.

46
03:45.760 --> 03:53.290
Now each action that you add will inherit from this particular class and that will allow you to create a

47
03:53.290 --> 03:56.100
whole bunch of actions, even if they don't get used.

48
03:56.090 --> 04:00.690
So remember: an action in the GOAP system can be added.

49
04:00.760 --> 04:07.840
It's not connected to anything at all, it's just sitting there as itself and it has Preconditions

50
04:08.080 --> 04:09.390
and it has Effects

51
04:09.640 --> 04:12.060
and then what happens when there's a Goal that comes in

52
04:12.060 --> 04:18.280
is that the Planner gathers up all of these Actions and tries to piece them all together, to see where the

53
04:18.280 --> 04:24.550
Preconditions and the Effects join together to make a chain, which becomes your Plan. So you can just

54
04:24.550 --> 04:28.120
add as many of these Actions onto your agent as possible

55
04:28.210 --> 04:33.700
but if they don't have Preconditions and Effects that will join with other actions, then they will never

56
04:33.700 --> 04:34.150
get used.

57
04:34.150 --> 04:39.540
So that's one of the things you really need to look out for when you're designing your Actions

58
04:39.550 --> 04:45.520
but we'll get around to that when we start making our own Actions and how important it is, so that they do

59
04:45.830 --> 04:47.060
chain together.

60
04:47.200 --> 04:54.180
The GoapPlanner itself gathers together all of the available Actions and checks them against the conditions,

61
04:54.190 --> 05:01.350
the state of the world and any particular Goals and comes up with your chained plan of actions, then reports

62
05:01.360 --> 05:09.270
that back to the agent that then uses the Finite State Machine to perform the actions along the chain.

63
05:10.140 --> 05:16.290
Now the code that we're interested in and the code that we write to interact with that GOAP system is

64
05:16.290 --> 05:22.890
currently on our Baker and that's the Worker script and the Inventory script. The Inventory script, if you

65
05:22.890 --> 05:27.420
drop it down in the Inspector, you'll see that it has levels for Flour and Bread.

66
05:27.420 --> 05:33.330
So these are the things that the agent has, it's like their 'backpack' or 'inventory'. If you have a look in

67
05:33.330 --> 05:35.870
that code, it's really quite simple.

68
05:35.940 --> 05:39.510
All it is, is some integers holding the values

69
05:39.510 --> 05:43.640
and we're starting off our agent as having 5 bags of flour.

70
05:43.830 --> 05:49.950
There's a really simple OnGUI there, just to print it on the screen, so we can see these values change

71
05:49.980 --> 05:52.400
as we run the simulation.

72
05:52.410 --> 06:01.710
Now the Worker code that's also on our agent is going to inherit from our GOAP interface here (IGoap), which is essentially

73
06:01.710 --> 06:03.070
our GOAP agent.

74
06:03.390 --> 06:08.340
It's got on it, obviously, our NavMeshAgent which we need to push it around.

75
06:08.340 --> 06:15.120
There's also a previousDestination Vector3 that's being stored for the reason of navigation, which you'll

76
06:15.120 --> 06:16.260
see in a moment

77
06:16.440 --> 06:21.350
and then we've also got a link to our inventory system that is on this agent.

78
06:21.360 --> 06:26.460
Those 2 things (the NavMeshAgent and the Inventory) are set up inside of the Start.

79
06:26.460 --> 06:29.070
So we grab hold of those 2 components,

80
06:29.160 --> 06:32.090
so we don't have to keep doing this throughout the code.

81
06:32.310 --> 06:41.400
Now, moving down through this code, we get to some of the most important parts of this agent and

82
06:41.400 --> 06:51.780
that is these 2 next methods: GetWorldState and CreateGoalState. The GetWorldState will inject into

83
06:51.780 --> 06:55.630
the GOAP system, the state of the world.

84
06:55.710 --> 06:59.910
Now for this, we're going to be using 'true' and 'false'

85
06:59.910 --> 07:07.040
states as we looked at originally in the previous lecture and we're going to set them in here.

86
07:07.050 --> 07:14.430
So this will keep getting run and updated for your agent so that the planning system can check if certain

87
07:14.430 --> 07:16.840
states have been achieved or not.

88
07:16.860 --> 07:27.000
Now in this case, initially, we're setting our World State 'hasFlour' to either True or False based on the

89
07:27.060 --> 07:33.330
inv.flourLevel. Before we continue, I'd like to explain the structure of the Preconditions and

90
07:33.330 --> 07:38.700
Effects, as they are essentially the glue that holds all of the Actions together in the chain.

91
07:38.880 --> 07:43.130
They are all stored in a HashSet in the format 'string, object'.

92
07:43.380 --> 07:49.190
In this case, we're representing them with a string that explains the State and then a True or a False.

93
07:49.380 --> 07:58.380
For example, ("hasFood", true) ("hasBread", false). You'll see these set for actions later on and they are truly

94
07:58.590 --> 08:06.450
the conditions that will bind each action together, so that for each actions Precondition, you need to

95
08:06.450 --> 08:14.850
make sure that you have another action that has an effect that will essentially magnetize them and match

96
08:14.850 --> 08:18.050
them together, so that they can form a chain.

97
08:18.180 --> 08:26.010
If you don't have those anywhere in your action set, then that action will not get used and you might find

98
08:26.010 --> 08:28.800
that the Planner cannot find a plan.

99
08:28.800 --> 08:36.600
So, back to our code. Where we put these Preconditions and Effects is inside of these 2 methods that we

100
08:36.600 --> 08:46.260
just talked about, between the initial declaration for the 'worldData' set and also for the 'goal' set.

101
08:46.260 --> 08:52.770
So this is all the states of the world, as I said. It gets updated all of the time, so this function gets

102
08:52.770 --> 08:59.300
called all of the time to keep checking that this 'hasFlour', for example, is either True or False

103
08:59.310 --> 09:04.110
and that's why we can put these variables in here, that we can change.

104
09:04.110 --> 09:11.130
This is the flour level in the inventory and it's not going to say that you've got any flour unless

105
09:11.460 --> 09:14.310
the flourLevel is greater than 1.

106
09:14.310 --> 09:19.590
So, in this case, it doesn't consider you've got enough flour to do anything with until you have 2 bags

107
09:19.650 --> 09:20.300
of flour.

108
09:20.310 --> 09:26.220
So that's setting the limit of how many bags of flour is considered 'having enough flour to bake

109
09:26.220 --> 09:26.730
with'.

110
09:26.730 --> 09:31.860
Now if you've got other conditions in World States that you want to do, that have a variable value in it,

111
09:32.190 --> 09:35.850
then you can add them inside of here.

112
09:36.210 --> 09:43.000
You cannot put the variable-type values like this inside of your Actions because it won't work.

113
09:43.000 --> 09:47.130
The Actions have to match up exactly when it goes to plan

114
09:47.430 --> 09:52.490
and if they don't, then you will get the whole system stalling.

115
09:52.500 --> 09:54.380
So, we'll see that in a moment anyway.

116
09:54.390 --> 09:56.070
So that's your worldData.

117
09:56.070 --> 09:59.160
Now the Goals themselves go in-between

118
09:59.190 --> 10:04.070
where you declare the set of goals and where you return them.

119
10:04.080 --> 10:10.770
So inside here, I want you to add a new goal for our Baker and it will be like this.

120
10:10.820 --> 10:16.280
So here's our little string here and here's our True or False value.

121
10:16.290 --> 10:20.850
Now this is a global goal for our agent and it's just going to be called

122
10:20.900 --> 10:22.890
"doJob", so "doJob"

123
10:23.030 --> 10:29.270
is True and it's going to keep forcing that on the agent and it'll want to keep making sure that it

124
10:29.270 --> 10:31.820
is addressing this particular goal.

125
10:31.910 --> 10:35.040
So we'll just have the 1 goal in here.

126
10:35.120 --> 10:39.570
So add that in and then further down let's have a look at the rest of the code.

127
10:39.590 --> 10:42.250
We've got the code for moving the agent.

128
10:42.320 --> 10:47.480
This is just typical NavMeshAgent-moving type code

129
10:47.500 --> 10:50.430
and what it does is a couple of things.

130
10:50.450 --> 10:56.890
It first of all checks for our previousDestination (remember that Vector3 at the top of this code), checks

131
10:56.900 --> 11:04.970
if that is not the same as where we need to go to next because if a NavMeshAgent is at its destination when

132
11:05.000 --> 11:12.020
it's given a new goal, then it won't plan a path and that becomes a bit of an issue for determining if

133
11:12.020 --> 11:16.840
we've reached our next destination to do our next task.

134
11:16.850 --> 11:24.260
So if the task is in the same position as the last task, then this will take care of that because the

135
11:24.260 --> 11:30.460
agent doesn't need to physically move around the environment, at which point we set

136
11:30.560 --> 11:32.750
nextAction.setInRange to true.

137
11:32.840 --> 11:41.350
Now each action is going to not run UNLESS your agent is within range of where that needs to happen.

138
11:41.360 --> 11:43.710
So that's what this line here is doing.

139
11:45.140 --> 11:50.810
Then, if we're not required to stay at the same destination (we need to go somewhere else), then we will

140
11:50.810 --> 11:57.860
use our NavMesh with SetDestination using the nextAction.target.transform.position to push us

141
11:57.860 --> 12:04.730
to the next location and then we're going to run checks like we have in the past with NavMeshes to

142
12:04.730 --> 12:06.740
check if we've reached our destination.

143
12:06.740 --> 12:15.560
So if we have a path and we're within a distance of 2, then we've reached our destination.

144
12:15.590 --> 12:19.450
This is basically your accuracy of how close you want to get

145
12:19.730 --> 12:20.900
and then we do the same thing.

146
12:20.900 --> 12:22.390
So we're in range

147
12:22.700 --> 12:28.040
and then we update our previousDestination, so that this just records where we are and we return True

148
12:28.040 --> 12:34.100
so that the system knows we're at our destination and we're ready to do the next action.

149
12:34.100 --> 12:34.310
All right.

150
12:34.310 --> 12:39.150
So moving down to the Update, you don't need anything inside the Update

151
12:39.170 --> 12:45.050
but what I've done here is just add a little bit of code which will help your agent slow down and

152
12:45.050 --> 12:51.620
speed up better into corners and you can actually apply this whole if-block to any Nav Mesh Agent code that

153
12:51.620 --> 12:52.400
you've got,

154
12:52.550 --> 12:55.660
if you want your agents to take tight corners.

155
12:55.790 --> 13:01.160
So I developed this for something else that I was working on and just thought I'd throw it in here.

156
13:01.160 --> 13:06.170
This works really nicely if you're trying to get your agents to move along streets and roads and you

157
13:06.170 --> 13:10.880
want them to stay on there and not veer-off onto the sidewalk, which is what they'll do if they're going

158
13:10.910 --> 13:12.500
quite fast.

159
13:12.590 --> 13:15.580
This will slow them down into corners for you,

160
13:15.590 --> 13:18.070
so take note of that

161
13:18.380 --> 13:25.480
and then further down we've got some methods that we aren't actually implementing whatsoever

162
13:25.850 --> 13:27.370
but we just need them in there

163
13:27.380 --> 13:31.940
otherwise it's going to complain and later on when you're more comfortable with this system, you could

164
13:31.940 --> 13:34.280
come back and put your own code in here.

165
13:34.280 --> 13:41.510
So, first of all, there's one that gets called when the plan fails...one when the plan is found...when your

166
13:41.510 --> 13:43.150
action has finished...

167
13:43.400 --> 13:49.160
and when your plan has aborted. So if you want to explore these, you could put Debug statements in here

168
13:49.160 --> 13:51.260
to see when they actually run

169
13:51.260 --> 13:54.060
but we're not going to be using them.

170
13:54.350 --> 13:54.680
Ok.

171
13:54.680 --> 14:01.430
At this point, if you press Play, you'll see in the console (if you go there) the messages that come out

172
14:01.430 --> 14:02.020
of the system.

173
14:02.030 --> 14:08.390
So these messages are in Debug statements throughout the GOAP library that you're using and you can find

174
14:08.390 --> 14:12.830
them if you have a look around but they're going to give you an idea of what's going on.

175
14:12.830 --> 14:17.640
So, the ones we're getting at the moment is that it's found actions.

176
14:17.690 --> 14:21.180
Now it hasn't actually found any actions because we haven't put any actions in there

177
14:21.200 --> 14:27.290
but it's still found an empty action set and then trying to do a plan that's going to

178
14:27.290 --> 14:32.660
satisfy that "doJob" goal and it can't find one, obviously, so it just does that (NO PLAN) and then it gives you this

179
14:33.050 --> 14:35.320
'Failed Plan' message.

180
14:35.390 --> 14:42.320
So you will know as you go along and program this system, when things aren't working. Ok, we'll leave this

181
14:42.440 --> 14:47.170
lecture here and in the next lecture we'll start creating actions for our agent.