1
00:00:00,150 --> 00:00:06,920
OK so now that we've got a core gameplay loop working we want to add something new to our enemies.

2
00:00:06,930 --> 00:00:13,710
So I want to do this because it's good to see that when you design your amnesty for game not everything

3
00:00:13,710 --> 00:00:18,510
is gonna make sense now everything will work the way you expected to do we made some enemies that seem

4
00:00:18,510 --> 00:00:22,770
like they made logical sense but there's a couple of things that become obvious when you start actually

5
00:00:22,770 --> 00:00:29,160
playing the game and it's OK to refine on your game after you've made some levels and discovered that

6
00:00:29,160 --> 00:00:31,290
something doesn't work quite the way you want to.

7
00:00:31,290 --> 00:00:38,530
And the thing I've noticed and you should have noticed at this point is that your enemies are a little

8
00:00:38,650 --> 00:00:42,730
bass a little bit slow to react.

9
00:00:42,730 --> 00:00:47,850
Shall we say so if you are out of range and you shoot an enemy.

10
00:00:48,190 --> 00:00:50,340
It just kind of stands there and gets shot.

11
00:00:50,410 --> 00:00:56,410
What we would really like to happen is that if the enemy gets shot it will start chasing the player

12
00:00:57,780 --> 00:01:01,580
so straight away it'll switch to his chase mode and come running to get to play.

13
00:01:01,800 --> 00:01:07,380
So we're going to set up a system to do that and it's very very easy to do what we're gonna do is first

14
00:01:07,380 --> 00:01:08,700
of all go to our enemy controlled.

15
00:01:08,700 --> 00:01:12,280
Ashley we'll go to our test scene because this is where we test things.

16
00:01:12,630 --> 00:01:13,960
So back in here.

17
00:01:13,960 --> 00:01:18,270
Let's double click on the enemy here so we can just see what's going on.

18
00:01:18,270 --> 00:01:22,800
And we're going to open up our Enemy Controller script and what we're gonna do is we're gonna scroll

19
00:01:22,800 --> 00:01:24,460
down to the bottom here.

20
00:01:24,460 --> 00:01:27,510
We're going to add a new public void

21
00:01:30,870 --> 00:01:38,980
that that will call we'll call it get shot and what this is gonna do is we're gonna scroll back up to

22
00:01:38,980 --> 00:01:49,090
the top and going to add a new bool variable so a previous bool that we'll call was shut and this function

23
00:01:49,090 --> 00:01:56,710
is just gonna say Well first of all it's gonna mark was shot is equal to true then it's going to say

24
00:01:56,710 --> 00:01:58,180
well we've been shot.

25
00:01:58,180 --> 00:02:07,870
So start chasing so we're gonna say chasing equals true and then we're going to say that we should start

26
00:02:07,870 --> 00:02:20,550
moving towards the player so Agent plus destination equals player controller does instance does transformed

27
00:02:20,550 --> 00:02:21,670
our position.

28
00:02:22,090 --> 00:02:30,190
Like so we'll just set that safe and then what we're gonna do is we're gonna call this function when

29
00:02:30,190 --> 00:02:31,850
our enemy gets damaged.

30
00:02:31,870 --> 00:02:39,100
So let's go back to our enemy health controller and in here whenever we take damage we're first of all

31
00:02:39,190 --> 00:02:45,960
gonna check and see if there is an enemy controller attached to this game object.

32
00:02:45,960 --> 00:02:51,130
So for example our targets will have an enemy health controller system but it won't have an enemy controller

33
00:02:51,130 --> 00:02:53,770
that we want to actually move.

34
00:02:53,770 --> 00:03:00,160
So what we're gonna do is at the top here we're gonna create a reference when I have a public enemy

35
00:03:00,160 --> 00:03:08,830
controller that will just call the easy for the enemy controller and we're gonna say here is if the

36
00:03:08,850 --> 00:03:12,160
Easy is not equal to no.

37
00:03:12,160 --> 00:03:17,980
So basically what we're gonna do is we're not in our inspector in unity we're gonna assign the Enemy

38
00:03:17,980 --> 00:03:20,500
Controller to this object if it has it.

39
00:03:20,500 --> 00:03:26,470
So if it's an enemy that can move will assign us here and then if it's not empty so if that means if

40
00:03:26,470 --> 00:03:32,690
it has an enemy controller then in here we're going to say okay on the Enemy Controller call the gusts

41
00:03:32,830 --> 00:03:34,780
or get shots.

42
00:03:34,870 --> 00:03:37,910
Function like so.

43
00:03:38,440 --> 00:03:44,870
Okay so let's just jump back into unity and we'll just set that up first of all and honor enemy.

44
00:03:45,320 --> 00:03:46,700
Let's go to the prefabs.

45
00:03:46,710 --> 00:03:51,500
I'm going to select the prefab of the enemy and then in the enemy held controller I'm gonna drop the

46
00:03:51,620 --> 00:03:53,870
Enemy Controller in there.

47
00:03:53,870 --> 00:03:55,050
So that's that setup.

48
00:03:55,160 --> 00:03:57,610
But then what should happen when we actually get shot.

49
00:03:57,640 --> 00:04:04,460
So at the moment we're telling it to go chasing after the player but if we scroll up here.

50
00:04:04,460 --> 00:04:06,370
So if we're not chasing we do all this stuff.

51
00:04:06,380 --> 00:04:07,790
And that's perfectly fine.

52
00:04:07,790 --> 00:04:11,000
But in here is where we handle if we are chasing.

53
00:04:11,450 --> 00:04:16,840
And most importantly here is how we tell it to stop chasing.

54
00:04:16,860 --> 00:04:17,150
Sorry.

55
00:04:17,270 --> 00:04:18,500
No not that one there.

56
00:04:18,500 --> 00:04:22,920
This one here is where we tell us to stop chasing as you can see it says chasing equals false.

57
00:04:22,970 --> 00:04:27,950
One thing I just occurred to me I don't really know why I'm setting the agent destination in that function

58
00:04:27,950 --> 00:04:32,780
down below because here is where we control where the enemy is going so I'm actually just going to remove

59
00:04:32,780 --> 00:04:35,920
that line and delete that down there.

60
00:04:35,930 --> 00:04:39,570
So it's go back up here to where our chasing is being set to false.

61
00:04:39,650 --> 00:04:43,370
And this happens when our enemy is above the distance to lose.

62
00:04:43,400 --> 00:04:49,340
So if we are too far away and we shoot the enemy it's still going to say oh well the players is too

63
00:04:49,340 --> 00:04:55,380
far away so just say chasing is false and I'm not going to try and go there but what we're gonna do

64
00:04:55,380 --> 00:05:01,780
instead is blow this we're gonna add an else statement

65
00:05:04,490 --> 00:05:11,140
and in here we're gonna say we know that we just set was shot to be true.

66
00:05:11,200 --> 00:05:16,320
So if we are above this distance we're gonna assume was shot is true.

67
00:05:16,330 --> 00:05:19,060
And once we get within this distance late range.

68
00:05:19,090 --> 00:05:23,920
So if the player gets within a certain distance to the enemy that's when we're gonna say was shot is

69
00:05:23,920 --> 00:05:25,030
false.

70
00:05:25,030 --> 00:05:25,730
At that point.

71
00:05:25,870 --> 00:05:31,570
And the reason we do this is because within this section here we're only gonna allow ourselves to stop

72
00:05:31,570 --> 00:05:38,100
chasing if was shot is false.

73
00:05:38,140 --> 00:05:40,360
So if Nuss was shot.

74
00:05:40,660 --> 00:05:43,060
So basically what happens is we'll come down here.

75
00:05:43,060 --> 00:05:47,380
We'll get shots and we're so far away from the enemy and it doesn't matter.

76
00:05:47,560 --> 00:05:50,350
Some far away from the player the enemy as we say was shot.

77
00:05:50,360 --> 00:05:51,580
It's true and chasing is true.

78
00:05:51,580 --> 00:05:53,590
So then it will go back up to here.

79
00:05:54,030 --> 00:05:59,590
And at this point it checks the distance to the player and the player the distance should be greater

80
00:05:59,590 --> 00:06:02,950
than distance to lose because it's the player so far away.

81
00:06:02,960 --> 00:06:03,300
And I'll go.

82
00:06:03,330 --> 00:06:07,150
Okay well I'll go in here but was shot is still true at the moment.

83
00:06:07,150 --> 00:06:10,200
So I can't stop chasing and what I see.

84
00:06:10,690 --> 00:06:15,370
I will put this whole thing inside the car brackets because it doesn't really matter whether we set

85
00:06:15,370 --> 00:06:17,410
this or not.

86
00:06:17,440 --> 00:06:22,050
So then we'll go down to here and say oh well this isn't true because this pit is already shit.

87
00:06:22,180 --> 00:06:28,720
And then when the player gets within this range that's when we'll say okay at this point we can stop

88
00:06:30,030 --> 00:06:31,990
marking was shot as true.

89
00:06:32,010 --> 00:06:37,320
So if the player gets within this range and then falls out of this range again that's when the enemy

90
00:06:37,320 --> 00:06:38,420
will stop chasing.

91
00:06:38,490 --> 00:06:41,380
So we can get within a certain distance to the enemy.

92
00:06:41,700 --> 00:06:44,590
And then once it gets close enough it'll say OK I know where the enemy is.

93
00:06:44,760 --> 00:06:47,070
I know where the player is going and keep shooting at him.

94
00:06:47,130 --> 00:06:53,430
And then when I lose sight lose distance to the player again that's when I stop chasing.

95
00:06:53,940 --> 00:06:54,950
So let's see this in action.

96
00:06:54,960 --> 00:06:59,250
I going to save this and we'll go back in here.

97
00:06:59,820 --> 00:07:03,210
I think at the moment our enemy might be a bit too close and he'll come chasing after us anyway.

98
00:07:03,230 --> 00:07:03,510
Yes.

99
00:07:03,600 --> 00:07:07,180
So that's just move the enemy away from us here.

100
00:07:07,230 --> 00:07:09,520
We'll put him a little bit further away there.

101
00:07:09,600 --> 00:07:10,980
So he's far enough away now.

102
00:07:11,000 --> 00:07:13,230
Well forget about our tired friend.

103
00:07:13,230 --> 00:07:15,100
Let's take their turn house.

104
00:07:15,610 --> 00:07:20,210
Now if I take a shot at our enemy here.

105
00:07:20,400 --> 00:07:23,860
Well it doesn't help when I give him a nice headshot.

106
00:07:23,980 --> 00:07:27,210
Let's I just disable our tires here for the moment.

107
00:07:27,430 --> 00:07:29,490
So let's just shoot this guy.

108
00:07:29,500 --> 00:07:32,610
Let's switch to our repeater and shoot him in the leg.

109
00:07:32,610 --> 00:07:36,780
And now he's gonna come running after me and he tries to shoot me.

110
00:07:36,780 --> 00:07:41,940
He keeps running against the wall there a little bit because we're stopping a couple of nav mesh issues

111
00:07:42,210 --> 00:07:44,380
with this guy at the moment but that's OK.

112
00:07:44,490 --> 00:07:45,390
We know how to solve that.

113
00:07:45,420 --> 00:07:48,020
It's just a problem within the unity Ed..

114
00:07:48,030 --> 00:07:52,070
It won't actually affect the final game but you can see he is correctly coming chasing after the player.

115
00:07:52,080 --> 00:07:58,940
So if I now move out of range because he was within range he's never gone back ever range again your

116
00:07:58,940 --> 00:08:03,360
way his five seconds and then he's asked back to his original position so perfect.

117
00:08:03,360 --> 00:08:07,340
We now have a way to get the enemy's attention when he gets shot.

118
00:08:07,410 --> 00:08:12,780
So it looks like oh no you've got my attention you're going to try and destroy me and we're pretty happy

119
00:08:12,870 --> 00:08:14,580
that that is working the way we expect.

