﻿1
00:00:04,260 --> 00:00:05,190
‫Hello and welcome.

2
00:00:05,190 --> 00:00:10,170
‫In this lecture, we're going to be getting a line of sight to our player from the enemy so that we

3
00:00:10,170 --> 00:00:17,100
‫are able, as the player, to hide behind obstacles and not have that enemy followers not have it.

4
00:00:17,100 --> 00:00:22,560
‫Keep aiming at us until we pop back out and are visible to them.

5
00:00:22,560 --> 00:00:25,890
‫So you can see here I've hidden behind a corner and he can't find me.

6
00:00:25,890 --> 00:00:29,400
‫Let's dive in and see how this can be done using unreal tools.

7
00:00:30,470 --> 00:00:35,120
‫So it's not ideal that this enemy A.I. can currently follow us round corners.

8
00:00:35,120 --> 00:00:40,720
‫No matter how far we go out, no matter how much we hide behind pillars, it's still going to find us.

9
00:00:40,720 --> 00:00:43,430
‫It's still going to come and keep aiming at us.

10
00:00:43,430 --> 00:00:47,990
‫We want to correct that, and we're going to do this by checking the line of sight from the enemy to

11
00:00:47,990 --> 00:00:48,680
‫the player.

12
00:00:48,680 --> 00:00:55,610
‫Now, as many things here in Unreal, there is already a function for this in the eye controller called

13
00:00:55,610 --> 00:00:57,260
‫line of sight.

14
00:00:57,260 --> 00:01:01,430
‫And if you go and search for that, you can find that there's a function that returns a boolean.

15
00:01:01,430 --> 00:01:06,440
‫So telling us whether or not we have line of sight and then it takes the actor that we should try to

16
00:01:06,440 --> 00:01:12,800
‫check for line of sight to, and it automatically takes into account things like the eye position of

17
00:01:12,800 --> 00:01:14,900
‫the pawn in question.

18
00:01:14,900 --> 00:01:20,300
‫All this stuff is already coded into Unreal is really cool, so it should be fairly straightforward.

19
00:01:20,300 --> 00:01:28,250
‫If we go over to our AI controller DHCP, what we basically want to do is change things up in TIC so

20
00:01:28,250 --> 00:01:32,630
‫that we first of all do an if line of sight.

21
00:01:33,420 --> 00:01:43,050
‫Then what we want to do if we've got line of sight, is we want to move to and we want to set focus.

22
00:01:43,380 --> 00:01:48,960
‫However, if we do not so in the else, what we want to do is clear focus.

23
00:01:48,960 --> 00:01:57,630
‫Remember, we saw that function before and we want to do the let's see the stop movement, basically.

24
00:01:58,560 --> 00:02:01,470
‫And that's another function that exists that we haven't seen yet.

25
00:02:01,590 --> 00:02:08,220
‫So what I'm going to do here is I can challenge you to implement the whole of this yourself if you feel

26
00:02:08,220 --> 00:02:09,000
‫confident.

27
00:02:09,120 --> 00:02:14,970
‫Otherwise, this else is the more complex bit that we haven't seen before because the other two, we've

28
00:02:14,970 --> 00:02:18,480
‫already done a move to actor, we've already done our set focus, we've done that before.

29
00:02:18,480 --> 00:02:22,500
‫And there's just this if line of sight, which we have just seen.

30
00:02:22,500 --> 00:02:23,460
‫It's pretty straightforward.

31
00:02:23,460 --> 00:02:24,420
‫You could implement that.

32
00:02:24,420 --> 00:02:25,620
‫But how about this else?

33
00:02:25,620 --> 00:02:29,070
‫Let's give a go at implementing the just the ELT clause.

34
00:02:29,070 --> 00:02:34,830
‫It's not going to compile because you obviously need an if before an else, but let's go and just write

35
00:02:34,830 --> 00:02:35,820
‫out the ELT clause.

36
00:02:36,180 --> 00:02:42,060
‫So the first thing clearing focus, basically we're saying stop trying to track the player because you

37
00:02:42,060 --> 00:02:43,110
‫can't see the player.

38
00:02:43,110 --> 00:02:47,610
‫You shouldn't be giving off the impression that you know where the player is, even though the eye obviously

39
00:02:47,610 --> 00:02:51,000
‫can, because it can access the player's location and so on and so forth.

40
00:02:51,000 --> 00:02:56,160
‫But we need to give the illusion that the AI only knows as much as it can see.

41
00:02:56,370 --> 00:02:59,550
‫So what we're going to do is we're going to call to clear focus.

42
00:02:59,550 --> 00:03:04,890
‫And if we go over to the AI controller H and have a look at clear focus, you can see that it takes

43
00:03:04,890 --> 00:03:07,410
‫in the priority that we need to clear.

44
00:03:07,410 --> 00:03:13,620
‫Now, a reminder that these priorities are basically different levels of focus.

45
00:03:13,620 --> 00:03:20,820
‫So we have typically default movement and gameplay gameplay being the highest priority thing.

46
00:03:20,820 --> 00:03:24,270
‫And what we set by default is gameplay priority.

47
00:03:24,270 --> 00:03:29,580
‫So what we want to do is clear the gameplay priority, which means that if we have focused based on

48
00:03:29,580 --> 00:03:37,080
‫movement, for example, if our eye then wants to move back to its guard point, then that focus will

49
00:03:37,080 --> 00:03:41,070
‫not be cleared and it will be off moving back to its guarding point.

50
00:03:41,430 --> 00:03:43,440
‫So that's the way focus works.

51
00:03:43,440 --> 00:03:45,660
‫That's the way the priority system works there.

52
00:03:45,660 --> 00:03:53,040
‫So what we just want to do is copy this a focus priority code on colon gameplay, and that is the priority

53
00:03:53,040 --> 00:03:55,440
‫that we're wanting to clear on this line here.

54
00:03:55,440 --> 00:03:58,020
‫And then the other thing is to stop the movement.

55
00:03:58,020 --> 00:04:03,150
‫So we want to go over to the AI controlled RH again and have a look at the stop movement.

56
00:04:03,150 --> 00:04:10,230
‫And sure enough, there is a simple no arguments function called stop movement, which allows us to

57
00:04:10,230 --> 00:04:11,730
‫do just that.

58
00:04:11,910 --> 00:04:12,780
‫So there you go.

59
00:04:12,780 --> 00:04:16,020
‫That is basically the statement written.

60
00:04:16,020 --> 00:04:23,070
‫So now just reminds it remains a challenge for you to implement the if portion with the if line of sight

61
00:04:23,070 --> 00:04:25,170
‫and the move to and set focus.

62
00:04:25,170 --> 00:04:27,750
‫And you basically want to remove this code here.

63
00:04:27,750 --> 00:04:33,000
‫I'm just going to comment it out and sort out where you want that to be.

64
00:04:33,000 --> 00:04:33,870
‫So pause video.

65
00:04:33,870 --> 00:04:35,130
‫No challenge, slide here.

66
00:04:35,130 --> 00:04:38,190
‫Just go and implement these three lines for yourself.

67
00:04:39,210 --> 00:04:39,690
‫Okay.

68
00:04:39,690 --> 00:04:40,270
‫Welcome back.

69
00:04:40,290 --> 00:04:42,150
‫So let's start with that.

70
00:04:42,150 --> 00:04:51,660
‫If so, if open brackets line of sight to and then we want to do line of sight to the actor, which

71
00:04:51,660 --> 00:04:54,360
‫is the player pawn that we want to get access to.

72
00:04:54,360 --> 00:05:00,900
‫So let's just uncomment that player pawn here and it's mucked up our indentation a little.

73
00:05:00,900 --> 00:05:02,160
‫Let's fix that.

74
00:05:02,490 --> 00:05:10,320
‫And then we want our brackets, our curly brackets here, and we want to do a move to actor.

75
00:05:10,320 --> 00:05:14,460
‫Well, in fact, we can just copy and uncomment our move to actor here.

76
00:05:15,230 --> 00:05:18,380
‫And we want to uncomment it.

77
00:05:18,380 --> 00:05:19,100
‫Yes, there we go.

78
00:05:19,100 --> 00:05:22,490
‫And the focus, we can move that as well.

79
00:05:22,520 --> 00:05:28,070
‫Now, the focus, it doesn't matter whether you do it before or after move to actor, because while

80
00:05:28,070 --> 00:05:34,280
‫move to actor does set a focus, it sets a lower priority, the movement focus rather than the gameplay

81
00:05:34,280 --> 00:05:34,640
‫focus.

82
00:05:34,640 --> 00:05:38,900
‫So it doesn't matter which order we do those two operations in.

83
00:05:38,990 --> 00:05:44,030
‫And then finally we can remove this commented out line where we were getting the player pawn in begin

84
00:05:44,030 --> 00:05:49,280
‫play because we no longer need it and everything's in tick now so we could remove begin play.

85
00:05:49,280 --> 00:05:53,840
‫I'm going to leave it around in case we need it later on just to save me the hassle of adding it back

86
00:05:53,840 --> 00:05:54,170
‫in.

87
00:05:54,170 --> 00:05:58,280
‫Let's head over to Unreal and compile and when that's done, let's play.

88
00:05:58,280 --> 00:06:00,710
‫So let's see if we can hide from this actor.

89
00:06:00,710 --> 00:06:04,370
‫I'm going to try and move around the corners reasonably quickly.

90
00:06:04,760 --> 00:06:09,170
‫This is a little bit tricky because they can move just as fast as you can, but if you get round a corner

91
00:06:09,170 --> 00:06:11,570
‫like that, they go, He's lost sight of me.

92
00:06:11,570 --> 00:06:13,310
‫He's stopped trying to chase me.

93
00:06:13,310 --> 00:06:16,550
‫I can just peek around the corner and see that as I'm moving.

94
00:06:16,550 --> 00:06:20,150
‫He's not changing his aiming because we've cleared the focus.

95
00:06:20,210 --> 00:06:22,370
‫Can peek around the corner from the other side.

96
00:06:22,640 --> 00:06:23,180
‫Let's see.

97
00:06:23,180 --> 00:06:24,560
‫Pick round over here.

98
00:06:24,590 --> 00:06:25,070
‫There you go.

99
00:06:25,070 --> 00:06:27,770
‫You can see he's still aiming at the last location where I was.

100
00:06:27,770 --> 00:06:34,220
‫But if I pop up into sight, then he carries on aiming and moves within the acceptance radius.

101
00:06:34,400 --> 00:06:38,900
‫Talking of which, I want that acceptance radius to be configurable by the designer.

102
00:06:38,900 --> 00:06:41,120
‫So let's move it into a new property.

103
00:06:41,240 --> 00:06:45,350
‫I'm going to create a private section here with a new property.

104
00:06:46,260 --> 00:06:48,330
‫Which is going to be an edit anywhere.

105
00:06:48,780 --> 00:06:58,830
‫And we want that to be a float called acceptance radius and give it the default value of 200.

106
00:06:59,100 --> 00:07:04,530
‫And then this acceptance radius we can go ahead and use in our move to actor.

107
00:07:04,530 --> 00:07:08,340
‫And if you compile that, it's going to give you exactly the same behavior, except now that you can

108
00:07:08,340 --> 00:07:11,940
‫change that in the AI controller blueprint.

109
00:07:11,940 --> 00:07:13,320
‫So that's it for this lecture.

110
00:07:13,320 --> 00:07:18,900
‫We have discovered that line of sight function, allowing us to check whether the eye can or cannot

111
00:07:18,900 --> 00:07:19,740
‫see something.

112
00:07:19,740 --> 00:07:25,740
‫And we've implemented this basic hiding functionality so the AI doesn't follow us when it can't see

113
00:07:25,770 --> 00:07:26,220
‫us.

114
00:07:26,250 --> 00:07:26,850
‫Next lecture.

115
00:07:26,850 --> 00:07:33,570
‫We're going to take things up a notch from this custom programmed AI and use behavior trees.

116
00:07:33,570 --> 00:07:39,000
‫A really powerful tool in unreal to make our AI's just that much cooler.

