﻿1
00:00:04,360 --> 00:00:05,260
‫Hello and welcome.

2
00:00:05,260 --> 00:00:08,800
‫In this lecture, we are going to be seeing things from the player's point of view.

3
00:00:08,830 --> 00:00:15,280
‫We will be getting hold of the players viewport, the location and rotation of the camera, essentially,

4
00:00:15,280 --> 00:00:21,010
‫so that when we hit the trigger button, we can draw a debug camera.

5
00:00:21,040 --> 00:00:26,890
‫These little red guys here, these little red boxy cameras that show us where that viewport is.

6
00:00:26,890 --> 00:00:28,060
‫And we can confirm that.

7
00:00:28,060 --> 00:00:32,050
‫We can then use that later on for raytracing for the gun.

8
00:00:32,050 --> 00:00:34,990
‫Let's dive in and see all of that in action.

9
00:00:36,200 --> 00:00:38,360
‫Let me start this lecture with a question.

10
00:00:38,360 --> 00:00:45,860
‫So if you are aiming through your viewport like this and you're trying to line up the center of your

11
00:00:45,860 --> 00:00:51,020
‫screen with that D on D command center or zero command center, whatever that's meant to be.

12
00:00:51,260 --> 00:00:54,860
‫And I aim what would I aim to?

13
00:00:54,950 --> 00:00:56,420
‫What would I expect to hit?

14
00:00:56,450 --> 00:00:59,360
‫You might say, well, I'd expect it to hit that zero.

15
00:00:59,360 --> 00:01:01,260
‫Sam, that seems like the obvious answer.

16
00:01:01,280 --> 00:01:06,140
‫Well, that's not necessarily what you are going to hit, depending on how you do things, how you do

17
00:01:06,140 --> 00:01:06,980
‫your recasting.

18
00:01:06,980 --> 00:01:10,160
‫So let's have a look at the strategies we have for recasting.

19
00:01:10,190 --> 00:01:15,410
‫The most obvious is you say, Well, we're firing out of the gun, so we're going to recast down the

20
00:01:15,410 --> 00:01:15,920
‫barrel of the gun.

21
00:01:15,920 --> 00:01:20,450
‫Or by the way, this set up, which is very, very crude, is basically our third person set up.

22
00:01:20,450 --> 00:01:21,860
‫We've got a camera back here.

23
00:01:21,860 --> 00:01:23,870
‫We've got the character, we've got the gun.

24
00:01:23,870 --> 00:01:26,690
‫And he's trying to shoot at another target character over here.

25
00:01:26,900 --> 00:01:31,240
‫So, well, first of all, we recast out of the gun like that pure.

26
00:01:31,280 --> 00:01:38,120
‫Maybe it misses because our camera is not pointing where the gun is pointing, not necessarily.

27
00:01:38,120 --> 00:01:41,630
‫Even though they might be pointing in the same direction, they're slightly offset from each other,

28
00:01:41,630 --> 00:01:43,970
‫so they won't necessarily hit the same things.

29
00:01:43,970 --> 00:01:48,440
‫Instead, you might choose that you want to recast from the camera.

30
00:01:48,470 --> 00:01:54,590
‫That means that whatever the camera is looking at is going to be the thing that is hit and that generally

31
00:01:54,590 --> 00:01:57,440
‫feels more natural in third person games.

32
00:01:57,710 --> 00:02:02,570
‫When you're in first person games, things get a little bit closer because often you're very close to

33
00:02:02,570 --> 00:02:02,960
‫the barrel.

34
00:02:02,960 --> 00:02:07,100
‫But even in first person games, this rule tends to be the case.

35
00:02:07,100 --> 00:02:10,970
‫You don't want to be moving around where the aiming point is on the screen.

36
00:02:10,970 --> 00:02:17,000
‫You typically have a little piece of UI that overlays here in the middle of your screen, and that's

37
00:02:17,000 --> 00:02:18,680
‫where you're going to aim down.

38
00:02:18,680 --> 00:02:25,010
‫And so that's why we tend to do our ray casting with ray casted weapons down this viewport.

39
00:02:25,340 --> 00:02:32,390
‫Now, fortunately, there's a way for us to get this viewport and we actually do it from the controller,

40
00:02:32,390 --> 00:02:34,520
‫we get the controller's viewport.

41
00:02:34,520 --> 00:02:41,720
‫So that requires us to go to the character, find the character's controller, and then find its viewport

42
00:02:41,720 --> 00:02:44,300
‫location and rotation.

43
00:02:44,300 --> 00:02:50,510
‫And what we're going to do in this lecture is try and visualize and confirm that that viewport location

44
00:02:50,510 --> 00:02:53,060
‫and rotation is what we would expect.

45
00:02:53,210 --> 00:02:57,350
‫And we're going to do that by drawing some debug helpers.

46
00:02:57,500 --> 00:03:05,390
‫Now the debug helpers are in a header file and the header files name is a debug.

47
00:03:06,310 --> 00:03:06,820
‫Helpers.

48
00:03:06,820 --> 00:03:10,600
‫Funnily enough, I believe or draw, draw debug helpers.

49
00:03:10,600 --> 00:03:11,170
‫That's the one.

50
00:03:11,530 --> 00:03:16,600
‫And if we go and have a look at this one F 12 to go navigate to a header file if you've already got

51
00:03:16,600 --> 00:03:22,120
‫it in your includes, we're going to look for a one called camera draw debug camera.

52
00:03:22,120 --> 00:03:26,890
‫And this draws something that looks a little bit like my diagram here, a little bit of a triangle or

53
00:03:26,890 --> 00:03:29,440
‫a nostrum that we can use.

54
00:03:29,440 --> 00:03:31,270
‫So what's it going to take in?

55
00:03:31,270 --> 00:03:37,870
‫It's going to take in the world a location of that camera, a rotation for said camera.

56
00:03:37,900 --> 00:03:41,320
‫So location, rotation, field of view.

57
00:03:41,320 --> 00:03:44,440
‫Well, we'll set that probably to a default like 90 degrees.

58
00:03:44,440 --> 00:03:46,960
‫There's a scale, there's a color defaults.

59
00:03:46,960 --> 00:03:48,130
‫These are all defaulted.

60
00:03:48,130 --> 00:03:49,720
‫So we can just use that defaults.

61
00:03:50,020 --> 00:03:58,000
‫So let's try draw a debug camera down here in my gun and we'll put it in the pull trigger method because

62
00:03:58,000 --> 00:03:59,410
‫that's why we're doing our work.

63
00:03:59,410 --> 00:04:01,870
‫It needs first of all, what was it?

64
00:04:01,870 --> 00:04:03,970
‫It was a new world.

65
00:04:03,970 --> 00:04:06,460
‫So we get our world.

66
00:04:07,300 --> 00:04:11,410
‫But we need to draw into the next thing would be a location.

67
00:04:11,410 --> 00:04:21,250
‫Let's just get actor location for the time being and do a get actor rotation as well.

68
00:04:22,030 --> 00:04:25,650
‫And then what was the final thing that we needed was a field of view.

69
00:04:25,660 --> 00:04:33,280
‫Let's just go for a 90 degree field of view, which is just a random thing there, and we can go ahead

70
00:04:33,280 --> 00:04:35,860
‫and compile and see whether this shows up.

71
00:04:36,280 --> 00:04:40,480
‫And then let's hit play and hit the.

72
00:04:41,170 --> 00:04:42,100
‫Trigger button.

73
00:04:42,100 --> 00:04:43,480
‫I'm not seeing anything here.

74
00:04:43,480 --> 00:04:47,110
‫Let's do an F eight and see if we can get closer.

75
00:04:48,550 --> 00:04:51,790
‫Still not showing any kind of camera in here.

76
00:04:51,790 --> 00:04:53,740
‫I'm wondering if it's really, really small.

77
00:04:54,490 --> 00:05:00,730
‫So one of the other options here is that we might want to have a persistent lines.

78
00:05:01,120 --> 00:05:07,660
‫And so that means we need to specify everything up to it in C++ so we can set our scale to one.

79
00:05:07,660 --> 00:05:13,180
‫We could even increase it maybe to a scale of two, just in case it's the size that's a problem.

80
00:05:13,240 --> 00:05:16,960
‫We can choose a color that's more visible, maybe red.

81
00:05:16,960 --> 00:05:19,000
‫So F color, colon, colon, red.

82
00:05:19,420 --> 00:05:27,760
‫And then we can try having persistent lines be true and try that one out for size.

83
00:05:28,410 --> 00:05:29,910
‫So compiling again.

84
00:05:30,890 --> 00:05:32,810
‫And jumping into play.

85
00:05:33,260 --> 00:05:34,960
‫And there we go.

86
00:05:34,970 --> 00:05:35,990
‫We hit.

87
00:05:37,450 --> 00:05:38,260
‫The trigger.

88
00:05:38,260 --> 00:05:45,730
‫And you can see that it shows up a little camera box here where we hit trigger and it's rotation and

89
00:05:45,730 --> 00:05:50,350
‫location are set by the guns and location and rotation.

90
00:05:50,350 --> 00:05:53,020
‫So now we want to try and get the viewport instead.

91
00:05:53,020 --> 00:05:57,220
‫So as I've already said, this is going to be on our controller.

92
00:05:57,250 --> 00:06:03,850
‫So let's look at Controller H and again, we want the one in the game framework and we're going to look

93
00:06:03,850 --> 00:06:06,640
‫for viewport in here.

94
00:06:06,770 --> 00:06:07,390
‫I beg your pardon?

95
00:06:07,390 --> 00:06:11,050
‫Actually, viewpoint is the word we're looking for and there's a few here.

96
00:06:11,050 --> 00:06:15,670
‫Get actor eyes a viewpoint that would be looking through the actor's eyes.

97
00:06:15,670 --> 00:06:22,240
‫But we actually want the get play of viewpoint, which for an eye controller will use their eyes, but

98
00:06:22,240 --> 00:06:24,490
‫for a player will use the player's camera.

99
00:06:24,490 --> 00:06:25,570
‫So that's what we want.

100
00:06:25,570 --> 00:06:27,700
‫We want the get player viewpoint.

101
00:06:27,910 --> 00:06:32,080
‫But first of all, we have to get hold of that controller in the first place.

102
00:06:32,080 --> 00:06:39,160
‫And the way to do that is, first of all, we need to get our owner remember that when we had our shooter

103
00:06:39,250 --> 00:06:46,420
‫character CP open and we go down to or go up to our Begin play, you can see we're setting the gun's

104
00:06:46,420 --> 00:06:48,190
‫owner to be the character.

105
00:06:48,400 --> 00:06:51,700
‫That means that in the gun we can go the other way.

106
00:06:51,700 --> 00:06:56,890
‫We can say get owner and that's going to get us the shooter character.

107
00:06:56,890 --> 00:06:59,380
‫Now it's not going to know that it's a shooter character.

108
00:06:59,380 --> 00:07:00,880
‫All it knows is that it is an actor.

109
00:07:00,880 --> 00:07:02,560
‫So we're going to have to do a cast here.

110
00:07:02,890 --> 00:07:07,810
‫So we'll do a cast to the A Shooter character.

111
00:07:08,540 --> 00:07:12,890
‫And in fact, we don't even need to know anything as much as the fact that it's a shooter character.

112
00:07:12,890 --> 00:07:18,830
‫It's sufficient to know that it is a pawn and any pawn can then possess this gun, even if it's not

113
00:07:18,830 --> 00:07:19,760
‫a shooter character.

114
00:07:19,760 --> 00:07:22,210
‫So try and be as nonspecific as possible.

115
00:07:22,220 --> 00:07:24,200
‫Why does it need to be a pawn, though?

116
00:07:24,200 --> 00:07:29,570
‫It needs to be a pawn because we need to get a controller and only pawns can be possessed by controllers.

117
00:07:29,570 --> 00:07:30,980
‫So let's put this in a variable.

118
00:07:30,980 --> 00:07:39,200
‫It's going to be a -- star and then owner -- is going to be the name of the variable.

119
00:07:39,710 --> 00:07:42,350
‫Then we need to check that this is not null.

120
00:07:48,750 --> 00:07:53,490
‫And I'm just going to do this with a one liner that says if it is null, we return early from our pool

121
00:07:53,490 --> 00:07:59,280
‫trigger so that we don't have any risk of trying to call anything with a null pointer.

122
00:07:59,430 --> 00:08:08,790
‫So then we can do a pawn owner, get controller and we want to save that in to the A controller.

123
00:08:09,030 --> 00:08:11,640
‫Star And this can be the owner.

124
00:08:12,090 --> 00:08:12,960
‫Controller.

125
00:08:14,210 --> 00:08:15,140
‫Like so.

126
00:08:15,470 --> 00:08:19,700
‫And again, we want to just check that this isn't null because we're going to be calling a method on

127
00:08:19,700 --> 00:08:23,930
‫that and we can't call methods on null pointers.

128
00:08:23,960 --> 00:08:26,930
‫It makes unreal, unhappy and crash.

129
00:08:26,930 --> 00:08:31,460
‫So this is where we can now do our owner controller.

130
00:08:33,340 --> 00:08:37,300
‫And we should be able to call to that get play a viewpoint.

131
00:08:38,420 --> 00:08:42,020
‫So here comes the challenge for you to set that location and rotation.

132
00:08:42,020 --> 00:08:45,440
‫Using that new function, you can see the signature for it.

133
00:08:45,440 --> 00:08:52,040
‫Again, we go to Controller H ahead of file and look at that get play a viewpoint.

134
00:08:52,040 --> 00:08:58,730
‫Try and see if from this signature you can work out how to use it to get that location and rotation

135
00:08:58,730 --> 00:09:01,460
‫that we want to pass in to our draw debug helper.

136
00:09:01,700 --> 00:09:05,690
‫And so you're going to remember that this is using out parameters.

137
00:09:05,690 --> 00:09:12,830
‫So revise your out parameters, draw the debug camera using that snippet that we've just written and

138
00:09:12,830 --> 00:09:14,270
‫test it in the viewport.

139
00:09:14,300 --> 00:09:19,400
‫Go and click the trigger button a few times in different locations and see where that camera ends up

140
00:09:19,400 --> 00:09:21,380
‫showing pause video and have a go.

141
00:09:22,470 --> 00:09:22,800
‫Okay.

142
00:09:22,800 --> 00:09:23,440
‫Welcome back.

143
00:09:23,460 --> 00:09:24,660
‫So get play a viewpoint.

144
00:09:24,660 --> 00:09:29,720
‫I'm going to copy the name of the method and bring it over here because it's an out parameter.

145
00:09:29,730 --> 00:09:34,080
‫I will need to declare first the variables that I want those values to go into.

146
00:09:34,080 --> 00:09:36,330
‫I've got an F vector location.

147
00:09:36,720 --> 00:09:41,340
‫We have got an F rotator rotation.

148
00:09:42,260 --> 00:09:45,660
‫And we want to pass those in as the arguments.

149
00:09:45,680 --> 00:09:48,320
‫Let me just fix up my indentation there a little bit.

150
00:09:48,320 --> 00:09:51,860
‫And we're going to pass in the location, the rotation.

151
00:09:52,190 --> 00:09:58,580
‫And we know that because it's just been declared here when reading this, we can guess that these are

152
00:09:58,580 --> 00:10:04,130
‫now out parameters, even though there isn't any syntax helpfulness from C++ on that regard.

153
00:10:04,460 --> 00:10:09,470
‫And then we can pass this location instead of the actor location, and we can pass this rotation instead

154
00:10:09,470 --> 00:10:13,820
‫of the active rotation into our draw debug camera.

155
00:10:14,210 --> 00:10:19,270
‫And we can go ahead and build that compile and try it out in the editor.

156
00:10:19,280 --> 00:10:21,350
‫Well, it looks like the compile is complete.

157
00:10:21,350 --> 00:10:23,120
‫Let's go and play.

158
00:10:23,450 --> 00:10:29,150
‫And when I hit Trigger, you could see a camera appearing right over my viewpoint.

159
00:10:29,150 --> 00:10:30,160
‫That's a good sign.

160
00:10:30,170 --> 00:10:32,510
‫If I step back a little bit, you can see there you go.

161
00:10:32,510 --> 00:10:38,300
‫We've got these cameras that are pointing in exactly the right direction from where I was just looking.

162
00:10:38,300 --> 00:10:43,700
‫So you can just imagine how we're going to be able to shoot a ray out of that camera down the end of

163
00:10:43,700 --> 00:10:49,040
‫the camera and try and hit something over there that I was looking at at the time.

164
00:10:49,040 --> 00:10:51,190
‫And that's what we're going to be doing in the next lecture.

165
00:10:51,200 --> 00:10:51,920
‫See you there.

