WEBVTT

0
00:11.930 --> 00:18.590
Welcome back to Part 3 of the series on creating a Finite State Machine for an NPC and what we're

1
00:18.590 --> 00:23.920
going to do now is add in our final state, which will be 'Attack'.

2
00:24.040 --> 00:31.610
So to begin with, we first need to set up our NPC to do that and it's going to need bullets.

3
00:31.610 --> 00:33.440
I'm going to go to the tank

4
00:33.440 --> 00:35.660
(let's find it in the scene)

5
00:35.660 --> 00:42.710
and we want to shoot from the tank. In the tank that you've been given (if you've downloaded my project)

6
00:43.310 --> 00:47.890
on the very end of the turret, if I can find it for you...

7
00:47.900 --> 00:50.280
let's just drop this down...

8
00:50.720 --> 00:55.190
you'll see that I've got a little cube just there.

9
00:55.190 --> 01:01.550
So I added a cube and I've rotated it and I've attached it to the tank at the position where I want the bullets

10
01:01.550 --> 01:02.960
to come from.

11
01:02.960 --> 01:07.880
Now you'll have to make sure you also turn off your Box Collider if you're doing this on a different

12
01:07.880 --> 01:08.580
model

13
01:08.720 --> 01:17.150
but what's important is that this box is attached to your NPC, so that when your NPC moves, this box does

14
01:17.150 --> 01:21.850
as well because it's the relative position for the bullets to come out of.

15
01:22.070 --> 01:23.990
So that's all ready to go there.

16
01:23.990 --> 01:25.950
Let's go back to the Animator

17
01:26.210 --> 01:29.140
and in here, we're going to add in our Attack script.

18
01:29.150 --> 01:41.690
So 'right-click > Create State > Empty' and we'll call this 'Attack' and for our attack state we will add the

19
01:41.690 --> 01:47.550
behavior of Attack and the code for our Attack

20
01:47.580 --> 01:48.890
looks like this.

21
01:49.190 --> 01:55.040
So I'm going to replace the commented-out code with the new code.

22
01:55.090 --> 02:02.230
Again Attack will take its lead from our NPCBaseFSM class that we created, so that it gets hold of the NPC

23
02:02.230 --> 02:10.330
data, then we start it up getting the information about the opponent and the NPC and the speed and all that

24
02:10.330 --> 02:13.370
(although it's not needed in this particular case)

25
02:13.570 --> 02:19.500
and we're going to call some methods that are inside the TankAI

26
02:19.990 --> 02:21.320
from this one instead.

27
02:21.320 --> 02:23.380
So we're calling back the other way this time

28
02:23.410 --> 02:27.780
and now we've got our TankAI StartFiring

29
02:27.880 --> 02:37.580
when we enter in the Attack state and then on the update, we will continue looking at the opponent.

30
02:37.600 --> 02:41.640
I'm just using a really easy transform.LookAt

31
02:41.640 --> 02:46.060
and then the opponent's position. You could put slerping and stuff in here if you want it to look a little

32
02:46.060 --> 02:47.410
bit more smoother

33
02:47.440 --> 02:50.360
but for our purposes, this works quite nicely.

34
02:50.620 --> 02:58.830
Then when we exit, we're going to run a TankAI method called StopFiring. The StartFiring (as you'll

35
02:58.830 --> 03:02.230
see in a minute) is an InvokeRepeating,

36
03:02.230 --> 03:08.880
so it's just going to start firing all the time while it's in the Attack state

37
03:09.010 --> 03:13.030
and then just before it leaves the Attack state, it will tell it to stop

38
03:13.150 --> 03:21.790
that firing-invoked method. In the TankAI script, we'll add in those methods that the state is calling.

39
03:21.790 --> 03:28.660
So just underneath GetPlayer, I'll add in these 3 things. The method doing all the work is this 'Fire'

40
03:28.660 --> 03:36.100
one and it's going to instantiate a bullet object from a prefab (that we'll set up in a moment) from its

41
03:36.100 --> 03:37.270
turret position

42
03:37.270 --> 03:42.580
and that's the little cube that we created before and the turret rotation. So it'll make sure that it pushes

43
03:42.580 --> 03:48.930
the bullet along in the same direction that the turret rotation is set to

44
03:49.180 --> 03:58.150
and it does this by using a GetComponent Rigidbody on the bullet and then an AddForce of the turrets

45
03:58.150 --> 04:01.240
forward direction multiplied by 500

46
04:01.240 --> 04:06.250
in this case. So this is the strength of the force that the bullet is coming out at.

47
04:06.250 --> 04:08.750
Now the 2 public methods here

48
04:08.800 --> 04:10.650
(for start and stop firing)

49
04:10.710 --> 04:16.140
are the ones that are called from back in the Attack script. StopFiring will

50
04:16.180 --> 04:22.330
just cancel the Invoke on 'Fire', which happens when StartFiring is called.

51
04:22.420 --> 04:27.440
So the InvokeRepeating will start the 'Fire' method (which is the one up here)

52
04:27.790 --> 04:36.400
after half-a-second and then it will continue calling it every half-second after that.

53
04:36.650 --> 04:39.990
Now you can make this faster by making this value here smaller

54
04:40.010 --> 04:43.070
or you could have the interval longer as well.

55
04:43.070 --> 04:47.330
Now the bullet itself needs to come through into the TankAI

56
04:47.340 --> 04:57.490
so we'll make a public exposed variable at the top for that...to drop it into. The game object also needs

57
04:57.490 --> 04:59.090
to know about its turret

58
04:59.080 --> 05:05.500
so we'll put GameObject because that's what we're using to set up the alignment of the bullet itself.

59
05:05.500 --> 05:14.320
So with those both public, we can save all of this code, switch back into Unity, find our tank (which is

60
05:14.410 --> 05:18.750
up the top here) and you'll see you've got a Bullet and the Turret. Now the Turret

61
05:18.760 --> 05:25.030
you have to get from the cube that's attached to the tank, so we grab that and drag it onto there

62
05:25.300 --> 05:31.480
and then the Bullet...you'll find the Bullet in the Assets from the project that I've given you.

63
05:31.540 --> 05:39.010
Now this is the same bullet that's in the tank project that Unity has but I've added some extra script

64
05:39.010 --> 05:43.260
onto it, so just drag-and-drop that into the place for the bullet.

65
05:43.330 --> 05:48.560
If you have a really quick look at the bullet and down the bottom there's a little bullet script here

66
05:48.730 --> 05:54.850
and what it does is it instantiates an explosion when the bullet hits something and then makes the bullet

67
05:55.030 --> 05:56.970
disappear essentially.

68
05:57.000 --> 05:57.220
Right.

69
05:57.220 --> 06:05.260
So that's already programmed for you in the resource file. Ok, so now we need to set up the attack so that

70
06:05.260 --> 06:09.080
the attack happens. If we're in chase mode,

71
06:09.700 --> 06:14.870
let's say we want to transition to Attack. Make a transition to Attack,

72
06:15.220 --> 06:21.830
when we are...let's say a distance of less than...10.

73
06:21.840 --> 06:29.710
So we've got a 'chase distance' of 20 and now we'll have an 'attack distance' less than 10.

74
06:29.880 --> 06:34.140
Now if it becomes greater than 10, then we'll go back to chasing.

75
06:34.560 --> 06:36.860
So Make Transition back the other way,

76
06:36.870 --> 06:38.220
select that transition

77
06:38.220 --> 06:43.980
and then we're going to add the condition of 'distance greater than 10'.

78
06:43.980 --> 06:49.200
So if we now get out of a range of 10, it will go back to chase.

79
06:49.230 --> 06:54.720
So 'less than' to Attack and 'greater than' to go back to the Chase mode.

80
06:54.780 --> 06:58.240
Let's have a look at the behavior of our tank now.

81
06:58.350 --> 07:06.410
So we're patrolling...the patrol itself brings us close enough to trigger the chase, so it'll go off and start

82
07:06.420 --> 07:07.450
chasing us.

83
07:07.560 --> 07:11.280
Once it gets close enough, it will stop and start firing,

84
07:11.280 --> 07:18.210
as you can see there. Now it doesn't keep moving towards us because that attack behavior has no movement

85
07:18.210 --> 07:19.690
code in it whatsoever.

86
07:19.740 --> 07:27.670
It only has turning code, so if I just move, it will keep trying to fire in my direction.

87
07:27.730 --> 07:32.880
Now if I get far enough away, it will go back into Chase and keep chasing me

88
07:33.160 --> 07:40.930
and then if I get really far enough away, it will forget about me entirely and go back into Patrol.

89
07:40.940 --> 07:47.230
Now what I don't have here is a transition that goes from Attack back to Patrol

90
07:47.260 --> 07:55.810
or the other way because this logic will fire straight through these states, depending on how close you

91
07:55.810 --> 08:02.680
are. If I suddenly got within 10, it would go via the Chase state into Attack and I wouldn't even have time

92
08:02.680 --> 08:05.460
to execute anything in the Chase.

93
08:05.530 --> 08:06.290
So that's fine.

94
08:06.310 --> 08:09.300
I don't need an extra one here, though

95
08:09.400 --> 08:11.580
you could put one in there if you wanted

96
08:11.650 --> 08:14.740
but in this case, it's absolutely not necessary.

97
08:14.740 --> 08:19.850
Obviously it's now attacking me off-screen because it's waypoint took me to close enough to my tank

98
08:19.870 --> 08:24.790
and you can see that over here in the Scene view, which is a lot of fun.

99
08:24.900 --> 08:28.860
Ok, so if I just move again and get out of the road,

100
08:29.240 --> 08:32.990
it should forget about me and it'll reset.

101
08:33.040 --> 08:40.540
It's entering of the patrol and go back to waypoint 1 and continue on its little route.

102
08:41.780 --> 08:45.110
All right, so that is Finite State Machines

103
08:45.140 --> 08:52.010
in a nutshell and how to employ the state machines that are inside of the mecanim system

104
08:52.160 --> 08:53.220
to do that.

105
08:53.390 --> 09:00.120
Now you can see how easy it is and how logical the Finite State Machine is

106
09:00.410 --> 09:07.310
and what it does over the old way of doing it, is there's a lot of if-statements

107
09:07.310 --> 09:12.410
left out of distances and things like that, that you would normally have to program into your

108
09:12.410 --> 09:16.920
MonoBehaviour to change the state of your character,

109
09:17.040 --> 09:24.430
whereas this is going to do it automatically for you in separate scripts that you can use on other characters

110
09:24.440 --> 09:31.270
and of course, define different types of behavior for those characters even if they're in the same state.