WEBVTT

0
00:11.290 --> 00:18.790
Thus far, we've added in many different behaviors for our little agent and we've also got the functionality

1
00:18.790 --> 00:24.100
to shoot at it and for its health to go down and its health to come up but we're not using that for

2
00:24.100 --> 00:25.000
anything.

3
00:25.000 --> 00:32.350
So let's go and add some more behavior methods into our AI script and then build behavior trees

4
00:32.350 --> 00:33.680
for them.

5
00:33.730 --> 00:36.740
So let's open up AI to begin with

6
00:37.000 --> 00:40.140
and we're going to add some more tasks.

7
00:40.180 --> 00:41.420
I'll just put them at the bottom

8
00:41.440 --> 00:45.510
for consistency, it doesn't really matter what order these are in.

9
00:45.520 --> 00:52.480
So we want to make the bot clever enough that it knows to flee when it's in trouble and it will be

10
00:52.480 --> 01:00.850
in trouble (obviously) when it's health gets low, but it will also remain in trouble if the player is chasing

11
01:00.850 --> 01:03.640
it or the player is close enough to it.

12
01:03.640 --> 01:10.330
So we want to be able to test for those conditions, as well as set it up to flee.

13
01:10.360 --> 01:13.390
Under here, let's add these methods in.

14
01:13.540 --> 01:20.620
First of all, I've got a method called 'IsHealthLessThan' and it will accept a 'health' float value.

15
01:20.620 --> 01:26.570
This is not the health that's stored in the agent class.

16
01:26.620 --> 01:32.020
This is health that we will pass through the from our Behavior Tree and we're going to test it against

17
01:32.110 --> 01:34.450
the health of the agent.

18
01:34.510 --> 01:41.150
It's going to return 'true' if the agent's health is less than the value being passed through.

19
01:41.590 --> 01:47.920
So therefore we've got this variable that we can put into the Behavior Tree to change the behavior

20
01:47.950 --> 01:49.020
of the agent.

21
01:49.180 --> 01:49.460
Ok.

22
01:49.480 --> 01:55.930
So you might have it that it doesn't want to flee away from danger unless it's health is at 20 or you might

23
01:55.930 --> 02:02.050
want it so it flees away from danger when its health is at 80, depending on how sensitive it is and then

24
02:02.050 --> 02:05.460
you can have different ones for different types of agents.

25
02:05.470 --> 02:11.760
Now besides checking for the health, we'll also test whether the bot is in danger or not

26
02:12.070 --> 02:18.090
and the 'InDanger' here is doing a similar thing, where it's taking a value that's coming from the

27
02:18.090 --> 02:18.640
Behavior Tree

28
02:18.640 --> 02:26.600
via this 'minDist' float and we'll test it against the distance between the player and the agent

29
02:26.800 --> 02:37.000
and if that distance is less then the minDist, then it IS in danger, it's still within range of the player.

30
02:37.030 --> 02:46.630
Finally, we've got another method called 'TakeCover' and what TakeCover is going to do is to work out

31
02:46.660 --> 02:48.220
'moving away from the player'

32
02:48.250 --> 02:56.530
and this is the same logic (the same vector calculation) we used in the crowd-fleeing, where we get a vector

33
02:56.530 --> 03:00.200
not TOWARDS the player but AWAY from the player

34
03:00.310 --> 03:07.260
and then we add that to the current agent's position and notice I'm multiplying it by 2.

35
03:07.270 --> 03:13.230
So it'll go twice as far away from the player than it currently is.

36
03:13.720 --> 03:18.470
Once we have a new destination, we can set the agent's destination here

37
03:19.530 --> 03:27.450
and as you might suspect, we will then use the MoveToDestination method that we've written a while

38
03:27.450 --> 03:32.150
ago and call that to actually get the agent to move.

39
03:32.190 --> 03:35.780
So that's your AI, save that.

40
03:35.840 --> 03:42.800
Now we'll come back into Unity and we will create a new script. So I'll just duplicate the Wander script and

41
03:42.800 --> 03:52.610
let's call that Flee.BT. Open that up and I'll add it to my collection of scripts that I'm getting.

42
03:52.970 --> 03:57.830
Now in here, we'll test for health being low and

43
03:57.860 --> 04:04.910
we'll test if we are within a certain danger radius and then we will also put in our 'sequence' which

44
04:04.910 --> 04:07.720
we need to TakeCover and then MoveToDestination.

45
04:07.910 --> 04:09.980
So it's going to look something like this.

46
04:10.780 --> 04:15.080
'while IsHealthLessThan(40.0)', in this case

47
04:15.340 --> 04:23.520
and also 'while InDanger(50.0)', so we're still within 50 distance of the player,

48
04:23.740 --> 04:29.850
then the 'sequence' will run for 'TakeCover' and 'MoveToDestination'.

49
04:29.870 --> 04:35.480
Save that and let's go and see how it makes our agent behave.

50
04:35.500 --> 04:43.300
So I'm going to select the agent and give it the new script of Flee...drag-and-drop that over into your

51
04:43.330 --> 04:51.910
Panda Behavior, press Play and then you'll have to go over to the agent and start firing on it to trigger

52
04:51.970 --> 04:58.630
the script to work because you need to be close to it AND it's health needs to be less than 40. So while

53
04:58.630 --> 05:04.040
you're doing it, if you keep an eye on its script, you'll see when it triggers.

54
05:04.390 --> 05:14.720
Now as long as the script stays below 40 for health, you can keep triggering it by staying near it and

55
05:14.720 --> 05:22.270
following it around and you'll find that the behavior itself is getting it to move away from you.

56
05:22.520 --> 05:29.450
Once it does reach over 40 again as it's going away, the whole thing will stop because you're negating

57
05:29.540 --> 05:34.940
this while-statement here and therefore it can't continue.

58
05:35.030 --> 05:40.550
You would therefore have to shoot at it again to make its health go below 40, to trigger this tree to

59
05:40.610 --> 05:41.280
happen again.

60
05:41.360 --> 05:46.280
Now that's all this agent is doing, it's not going to turn around and attack you back because you've

61
05:46.460 --> 05:49.300
not got that in this particular script.

62
05:50.390 --> 05:57.820
Another thing we can use our IsHealthLessThan method for is to test whether our bot has died

63
05:57.920 --> 06:00.770
and for that we need another behavior script.

64
06:00.770 --> 06:07.080
So again, I'll just copy the Wander and let's call that

65
06:07.140 --> 06:08.300
Dead.BT

66
06:08.600 --> 06:19.780
and instead of a 'sequence', we'll have a 
'while IsHealthLessThan(0.1)', which is a very, very

67
06:19.780 --> 06:21.210
minimal health.

68
06:21.250 --> 06:24.430
In that case, our bot will be dead.

69
06:24.640 --> 06:28.980
So what do we do with it when it's dead? Let's explode it.

70
06:30.070 --> 06:36.390
'Explode' is not a method that comes with the behavior tree and it's something that you have to write yourself.

71
06:36.610 --> 06:42.660
So we'll save this and then we'll go back to our AI script and add in an 'Explode' method.

72
06:42.670 --> 06:44.230
We'll do that down the bottom.

73
06:44.230 --> 06:55.420
So...'Explode'...all it's going to do is to destroy the healthBar game object and then destroy the game object

74
06:55.630 --> 07:02.830
of the agent itself (and then it will just be gone from the game). If you wanted an explosion effect particle

75
07:02.830 --> 07:06.110
system, you would add it into here.

76
07:06.280 --> 07:16.360
Again, this is a bool-returning method with a return of 'true' and there's no reason why this couldn't

77
07:16.360 --> 07:24.400
be used as a 'void' with a Task.current.Succeed, unless somewhere in your tree you wanted to test

78
07:24.640 --> 07:29.950
if the explosion had occurred and then deal with it that way.

79
07:29.950 --> 07:38.460
Ok, so save that, go back into Unity, get your 'Dead' script and you want to attach that as the Behavior Tree

80
07:38.470 --> 07:47.740
for your Droid. Let's put it there and then we'll press Play and we'll come over and shoot until its health goes down

81
07:47.740 --> 07:54.310
to 0 to test it. So...shoot and then it's gone.

82
07:54.490 --> 07:56.730
So that was the Explode.

83
07:57.740 --> 08:05.480
All of the behavior trees that we've created are all well and good by themselves but when they really

84
08:05.480 --> 08:11.930
come into play is when we put them all together and make a much more complex behavior tree to represent

85
08:12.260 --> 08:18.850
the entire behavior of our agent and we'll do that in the next lecture.