﻿1
00:00:04,350 --> 00:00:05,040
‫Hello and welcome.

2
00:00:05,040 --> 00:00:11,340
‫In this lecture, we're going to be getting at eye to followers and navigate around the world, avoiding

3
00:00:11,340 --> 00:00:15,030
‫obstacles and just generally being a little bit of an annoying nuisance.

4
00:00:15,030 --> 00:00:17,520
‫Let's dive in and see how this is possible.

5
00:00:18,360 --> 00:00:25,200
‫So the next most obvious, obvious piece of the puzzle that we need to sort out here is getting our

6
00:00:25,440 --> 00:00:27,460
‫eye to move towards us.

7
00:00:27,480 --> 00:00:29,100
‫It's no good them just standing there.

8
00:00:29,130 --> 00:00:31,770
‫They also need to move and then they'll need to shoot.

9
00:00:31,770 --> 00:00:34,800
‫First of all, let's figure out how we can do movement.

10
00:00:35,340 --> 00:00:41,550
‫Now, when you're moving with eye needs to know about obstacles, it needs to do something called pathfinding

11
00:00:41,550 --> 00:00:45,210
‫because say, I'm hiding down in this corridor here and it wants to get to me.

12
00:00:45,240 --> 00:00:48,540
‫It needs to know that it can't just move through the wall.

13
00:00:48,540 --> 00:00:53,880
‫It's going to need to plan its route to come around the obstacles and come towards me.

14
00:00:54,210 --> 00:01:01,260
‫Now, the way this typically works in games in general and specifically here in Unreal is that we generate

15
00:01:01,260 --> 00:01:08,670
‫a mesh which tells us where is walkable within a level and then using various algorithms, one of which

16
00:01:08,670 --> 00:01:10,440
‫is called a star path finding.

17
00:01:10,440 --> 00:01:18,190
‫You can navigate on this mesh and create a route plan and the AI then uses this route plan to move along.

18
00:01:18,210 --> 00:01:20,360
‫So that's the kind of outline of the process.

19
00:01:20,370 --> 00:01:22,260
‫Fortunately, a lot of that is done for us.

20
00:01:22,260 --> 00:01:24,020
‫We don't have to code this from scratch.

21
00:01:24,030 --> 00:01:28,290
‫So what we need to do is, first of all, be able to see the nav mesh.

22
00:01:28,290 --> 00:01:32,780
‫And at the moment we can't see enough mesh partially because there isn't one.

23
00:01:32,790 --> 00:01:38,490
‫So what we need to do is look in our placed actors window here, or so we go to window and look for

24
00:01:38,490 --> 00:01:40,260
‫placed place actors.

25
00:01:40,620 --> 00:01:47,190
‫We need to search for a nav mesh and we're looking for the nav mesh bounds volume.

26
00:01:47,190 --> 00:01:54,090
‫Now this creates a volume, a kind of box, and everything within that box is going to be checked for

27
00:01:54,090 --> 00:01:58,380
‫how navigable it is, basically, whether it's flat or whether it's not too steep.

28
00:01:58,950 --> 00:02:03,300
‫Basically where there's a wall, you can't go through walls, you can't walk over walls because they're

29
00:02:03,300 --> 00:02:04,010
‫too steep.

30
00:02:04,020 --> 00:02:09,090
‫So if we drag in a nav mesh bounds volume and just bring it here and just put it slightly through the

31
00:02:09,090 --> 00:02:16,590
‫floor so that this allows the floor to be part of that navigation calculation, then what we've got

32
00:02:16,590 --> 00:02:21,120
‫to do is enable the visibility of NAV meshes.

33
00:02:21,120 --> 00:02:25,890
‫You can do this by clicking on the show button in the top left of your viewport and then clicking on

34
00:02:25,890 --> 00:02:28,110
‫navigation or using the shortcut.

35
00:02:28,110 --> 00:02:29,880
‫P So I'm going to use the shortcut.

36
00:02:29,880 --> 00:02:33,870
‫P And selecting well, you don't even have to select the bounds volume.

37
00:02:33,870 --> 00:02:39,270
‫You can instantly see this green mesh has appeared within the bounds volume which is telling us this

38
00:02:39,270 --> 00:02:40,800
‫is a walkable area.

39
00:02:40,800 --> 00:02:48,300
‫So if we had our enemy within this area and his target was also within the area, then it could find

40
00:02:48,300 --> 00:02:50,580
‫a path along this mesh.

41
00:02:50,760 --> 00:02:55,200
‫Now let's have a look at what happens if we move this to where there's an obstacle.

42
00:02:55,200 --> 00:03:00,060
‫You can see that the mesh kind of cuts out around this obstacle.

43
00:03:00,060 --> 00:03:07,680
‫So what we actually want to do is have this volume encompass the whole of our level and you could choose

44
00:03:07,680 --> 00:03:14,100
‫not to or just do it in certain segments, but the easiest way is just to increase the bounds of this

45
00:03:14,100 --> 00:03:15,780
‫volume, which you can do.

46
00:03:15,780 --> 00:03:20,310
‫If you go over to the details pane on the right, there's the brush settings and you can change the

47
00:03:20,310 --> 00:03:26,850
‫size of the brush like so you can see we can make it larger, we can make it wider and it goes red when

48
00:03:26,850 --> 00:03:28,770
‫it's being recalculated.

49
00:03:28,770 --> 00:03:33,300
‫You can see it's doing that building nav mesh and the larger it's going to be, the harder it is going

50
00:03:33,300 --> 00:03:35,640
‫to be to build a nav mesh longer it's going to take.

51
00:03:35,640 --> 00:03:37,590
‫But you can see that this is working.

52
00:03:37,590 --> 00:03:39,210
‫It's cutting out the corridors.

53
00:03:39,210 --> 00:03:43,000
‫It's cutting out the obstacles that we can't walk around.

54
00:03:43,020 --> 00:03:52,170
‫The second part of this puzzle is having our eye able to do its rooting algorithms on this mesh.

55
00:03:52,200 --> 00:03:56,970
‫Now, let's go ahead and open the BP shooter eye control, because I want to show you something that

56
00:03:56,970 --> 00:03:57,930
‫is in here.

57
00:03:57,960 --> 00:04:02,370
‫I'm going to open up the full blueprint editor here and have a look here.

58
00:04:02,370 --> 00:04:06,180
‫There is a component called the path following component.

59
00:04:06,360 --> 00:04:13,320
‫That component is responsible for finding an F mesh and then basically allowing us to create paths and

60
00:04:13,320 --> 00:04:16,230
‫follow them and move along them automatically.

61
00:04:16,230 --> 00:04:19,080
‫So we don't have to build any of that functionality in.

62
00:04:19,080 --> 00:04:24,300
‫All we have to do is use a function that's available on our AI controller.

63
00:04:24,300 --> 00:04:33,540
‫If you go and search for the AI controller H and look for the move to function, then let's do move

64
00:04:33,540 --> 00:04:35,490
‫to with an open brackets.

65
00:04:35,490 --> 00:04:37,200
‫Then there's a couple of options here.

66
00:04:37,200 --> 00:04:40,350
‫We've got a one takes a move to request.

67
00:04:40,470 --> 00:04:45,510
‫There are some other options here too, but they actually go by a different name.

68
00:04:45,510 --> 00:04:52,140
‫Now, in later versions of Unreal, we've got move to actor and we've got move to location, which have

69
00:04:52,140 --> 00:04:55,350
‫the same distinction as our setting focus.

70
00:04:55,350 --> 00:05:00,630
‫You can set focus to something that's moving like an actor, or you can set focus to a specific destination

71
00:05:00,630 --> 00:05:06,420
‫such as a waypoint in your patrol route, for example, might be a move to location.

72
00:05:06,420 --> 00:05:10,680
‫So I'd like you to have a go at attempting a move to as your challenge.

73
00:05:10,680 --> 00:05:16,560
‫So get a nav mesh in your scene, make it cover as much of the scene as you need to for you to be able

74
00:05:16,560 --> 00:05:17,850
‫to move around and have the.

75
00:05:17,910 --> 00:05:23,640
‫I follow you in all locations and then call that move to either move to act or move to a location.

76
00:05:23,640 --> 00:05:29,300
‫You decide which inn begin play to get the AI chasing us.

77
00:05:29,310 --> 00:05:31,110
‫So pause the video and have a go.

78
00:05:32,330 --> 00:05:32,750
‫Okay.

79
00:05:32,750 --> 00:05:33,290
‫Welcome back.

80
00:05:33,350 --> 00:05:34,470
‫Let's give this a shot.

81
00:05:34,490 --> 00:05:35,870
‫So first things first.

82
00:05:35,870 --> 00:05:44,660
‫We're going to have I'm going to be using a move to actor and we're going to have to set the goal actor.

83
00:05:44,660 --> 00:05:51,920
‫So let's go over to our shooter I controller CP and in there begin play we can do move to actor and

84
00:05:51,920 --> 00:05:57,290
‫we've got an actor already the player -- which we've got from the last lecture and let's just have

85
00:05:57,290 --> 00:05:57,590
‫a look.

86
00:05:57,590 --> 00:06:00,350
‫Are there any other useful properties here?

87
00:06:00,350 --> 00:06:01,940
‫We've got an acceptance radius.

88
00:06:01,940 --> 00:06:04,010
‫At the moment it has no acceptance radius.

89
00:06:04,010 --> 00:06:06,140
‫Basically it gets as close as it possibly can.

90
00:06:06,140 --> 00:06:08,870
‫I think that might be a little bit encroaching.

91
00:06:08,870 --> 00:06:14,330
‫I'm going to give it an acceptance radius of two meters, so it will try to get within two meters of

92
00:06:14,330 --> 00:06:14,750
‫us.

93
00:06:15,110 --> 00:06:16,910
‫So let's go ahead and build that.

94
00:06:17,690 --> 00:06:20,600
‫And once that's complete, go over into the engine.

95
00:06:20,600 --> 00:06:21,740
‫And let's have a look.

96
00:06:21,740 --> 00:06:28,820
‫I'm going to not expand the bounce volume here just to give you a taste of why we need the bounce volume

97
00:06:28,820 --> 00:06:29,690
‫in the first place.

98
00:06:29,690 --> 00:06:32,120
‫So let's start off with an alt p.

99
00:06:32,780 --> 00:06:33,530
‫And here we are.

100
00:06:33,530 --> 00:06:38,000
‫We're on the nav mesh and this guy has moved towards us automatically.

101
00:06:38,000 --> 00:06:45,380
‫But if I leave that nav mesh, he can no longer get any closer to me because he can't find the route.

102
00:06:45,380 --> 00:06:48,980
‫He can't find a way of getting to me.

103
00:06:48,980 --> 00:06:55,100
‫So what we're going to do is we in fact, I'm not sure that was what's actually going on here, because

104
00:06:55,100 --> 00:07:00,950
‫probably what's happening is that he's getting within the acceptance radius of me and they're not trying

105
00:07:00,950 --> 00:07:01,550
‫again.

106
00:07:01,550 --> 00:07:09,860
‫So one way to check that would be to just remove this acceptance radius and recompile and then we can

107
00:07:09,860 --> 00:07:12,620
‫go over and see if that's what's going on.

108
00:07:12,620 --> 00:07:16,340
‫So yeah, he comes right up to me if I move away.

109
00:07:16,340 --> 00:07:19,130
‫Well, he basically forgot that command.

110
00:07:19,130 --> 00:07:25,460
‫So what we want to do is do this in tic, because basically once he reaches me, he gives up on the

111
00:07:25,460 --> 00:07:26,720
‫idea of chasing me.

112
00:07:26,720 --> 00:07:28,940
‫So let's move this over to tic.

113
00:07:28,940 --> 00:07:31,580
‫This isn't the ideal way to do it, but that's what we're going to do.

114
00:07:31,580 --> 00:07:38,030
‫Just for now, I'm going to copy the signature of Tic from actor H, and I know that it goes in the

115
00:07:38,030 --> 00:07:40,820
‫public section, so I'm going to create a public section at the top.

116
00:07:42,770 --> 00:07:50,210
‫Copying the signature, make it an override, just safety, and then implement it over in the C++ and

117
00:07:50,210 --> 00:07:57,980
‫move our move actor in to tick and the acceptance radius still of two metres that's try this now and

118
00:07:57,980 --> 00:08:01,370
‫obviously it's complaining that we don't have a player -- over there.

119
00:08:01,370 --> 00:08:06,530
‫So let's go and make sure we're getting hold repeating this line that gets hold of the first player

120
00:08:06,530 --> 00:08:09,230
‫-- over in attic as well.

121
00:08:09,230 --> 00:08:14,180
‫And now we should be able to test it out properly, do an alt pig into the world.

122
00:08:14,180 --> 00:08:16,250
‫He comes and gets us within two meters.

123
00:08:16,250 --> 00:08:18,710
‫I move away from him and I'll look at that.

124
00:08:18,710 --> 00:08:24,470
‫He's not even aiming at me properly this time, but he is trying to follow along.

125
00:08:24,740 --> 00:08:29,630
‫But as soon as I leave that nav mesh, he no longer knows how to find me.

126
00:08:29,630 --> 00:08:31,190
‫So let's fix that up, shall we?

127
00:08:31,190 --> 00:08:35,450
‫We're going to go to an nav mesh bounce volume.

128
00:08:36,250 --> 00:08:37,390
‫If I can select it.

129
00:08:37,390 --> 00:08:37,990
‫There we go.

130
00:08:37,990 --> 00:08:44,170
‫And then we want to really zoom out here to try and encompass the whole of our level.

131
00:08:44,180 --> 00:08:49,210
‫It looks a bit janky on the outside this level, but that's just the way things often work, is that

132
00:08:49,210 --> 00:08:52,990
‫you build the level to be viewed from the inside, not from the outside.

133
00:08:52,990 --> 00:08:55,480
‫So let's make this much wider.

134
00:08:55,480 --> 00:09:01,450
‫Let's make it much taller so that it encompasses all of the level and just drag it out so that all the

135
00:09:01,450 --> 00:09:04,000
‫extents of the level here are covered.

136
00:09:04,000 --> 00:09:11,920
‫Just make it a little bit wider, a little bit longer too, and just make sure all of this is encompassed

137
00:09:11,920 --> 00:09:12,790
‫fully.

138
00:09:13,150 --> 00:09:16,690
‫And you can see the green has appeared as it's calculated the nav mesh.

139
00:09:16,690 --> 00:09:22,120
‫And let's just make sure that the nav measure is going down low enough that it's covering the lowest

140
00:09:22,120 --> 00:09:26,140
‫extents of our map seems to be covering it now.

141
00:09:26,140 --> 00:09:33,760
‫So if we go ahead and we can hide and nav mesh now that we have set it all up and we can do an alt p

142
00:09:33,760 --> 00:09:40,480
‫to go ahead and play and he can find us and follow us pretty much anywhere that we go within this level.

143
00:09:40,780 --> 00:09:47,830
‫You can see that our trusted loyal servant is following along behind us wherever we choose to go.

144
00:09:47,860 --> 00:09:51,040
‫He is just tailing us and trailing us, which is very annoying.

145
00:09:51,040 --> 00:09:53,830
‫But he is able to do that.

146
00:09:54,590 --> 00:09:55,190
‫Well, some stuff.

147
00:09:55,190 --> 00:10:02,450
‫So in this lecture we have seen how we can create our NAV measures and how we can use them to get our

148
00:10:02,450 --> 00:10:05,450
‫air to follow us around the level.

149
00:10:06,490 --> 00:10:12,220
‫Next lecture we're going to loop back around to this aming fix the bug and check line of sight so that

150
00:10:12,220 --> 00:10:16,330
‫we don't necessarily always chase the player around.

151
00:10:16,330 --> 00:10:17,050
‫See there.

152
00:10:18,390 --> 00:10:22,860
‫And I've just noticed one silly little mistake that I have made here, which is that I'm not calling

153
00:10:22,860 --> 00:10:25,620
‫to super colon colon tic.

154
00:10:25,620 --> 00:10:31,860
‫So that is actually why when you sometimes you get these weird unexplained bugs, if you're not calling

155
00:10:31,890 --> 00:10:35,160
‫to super, then things don't always work as expected.

156
00:10:35,160 --> 00:10:41,250
‫That's why the focus had stopped working, because essentially we had disabled tic on a high controller,

157
00:10:41,250 --> 00:10:42,270
‫which is no good.

158
00:10:42,330 --> 00:10:47,520
‫If you enable that, then everything should work as normal with the setting focus.

