﻿1
00:00:04,080 --> 00:00:05,730
‫Hello and welcome back.

2
00:00:05,760 --> 00:00:10,380
‫Now we're ready to rotate the turret and we'd like to do this using the mouse.

3
00:00:10,380 --> 00:00:14,790
‫And that hit location from the trace hit we've done in the previous video.

4
00:00:14,880 --> 00:00:22,200
‫So as I'm moving my mouse cursor, the turret is rotating and facing the direction of the mouse cursor.

5
00:00:22,320 --> 00:00:28,110
‫And even when I click in the viewport and begin to play, I can still move the mouse cursor around to

6
00:00:28,110 --> 00:00:31,080
‫rotate the turret and set its direction.

7
00:00:31,110 --> 00:00:35,850
‫This will allow me to aim when I have the capability of firing projectiles.

8
00:00:35,880 --> 00:00:36,200
‫Okay.

9
00:00:36,240 --> 00:00:41,770
‫So to achieve this, we need to find the direction that we want our turret to point in.

10
00:00:41,790 --> 00:00:46,410
‫And once we've found that direction, then we can set the turret mesh rotation.

11
00:00:46,500 --> 00:00:52,470
‫Now, in order to get the direction that we'd like the turret to point in, we need to do a little bit

12
00:00:52,470 --> 00:00:53,500
‫of vector math.

13
00:00:53,520 --> 00:00:59,570
‫What we're looking for is a vector from the tank turret to our trace hit location.

14
00:00:59,580 --> 00:01:05,940
‫Now we can easily get the location of our tank or any of its components, such as the location of the

15
00:01:05,940 --> 00:01:06,960
‫tank's turret.

16
00:01:06,990 --> 00:01:09,600
‫We'll call this location, turret, location.

17
00:01:09,600 --> 00:01:13,440
‫And we also know how we can get the location of the trace head.

18
00:01:13,470 --> 00:01:15,800
‫We'll call this trace hit location.

19
00:01:15,810 --> 00:01:20,760
‫What we're interested in is the vector from turret location to trace hit location.

20
00:01:20,760 --> 00:01:23,100
‫We'll call this vector to target.

21
00:01:23,130 --> 00:01:27,030
‫Now the vector math to get to target is actually quite simple.

22
00:01:27,060 --> 00:01:33,120
‫To get a vector from a start point to an endpoint is always the endpoint minus the start point.

23
00:01:33,420 --> 00:01:39,270
‫So if we take our trace, hit location and subtract the vector turret location, then the vector that

24
00:01:39,270 --> 00:01:41,620
‫results from this is to target.

25
00:01:41,640 --> 00:01:47,160
‫Now, once we have the two target direction vector, we want to convert that to a rotation so we can

26
00:01:47,160 --> 00:01:49,530
‫set the rotation of the tank's turret.

27
00:01:49,530 --> 00:01:53,750
‫And the vector type has a built in function called rotation.

28
00:01:53,760 --> 00:02:01,230
‫So if we took our to target vector and called rotation on it, this would return an F rotator corresponding

29
00:02:01,230 --> 00:02:02,260
‫to that direction.

30
00:02:02,280 --> 00:02:04,430
‫Now we do need to keep something in mind.

31
00:02:04,440 --> 00:02:11,160
‫The trace hit result is often going to be on the floor and the vector from the tank turret to the trace

32
00:02:11,160 --> 00:02:14,620
‫hit result is not going to be parallel with the ground.

33
00:02:14,640 --> 00:02:20,850
‫This means that if we set the tank's turret rotation to the rotator we get from that direction vector,

34
00:02:20,850 --> 00:02:26,790
‫we might get something like this where the turret is angled down towards the ground, but we don't want

35
00:02:26,790 --> 00:02:29,310
‫the tanks turret to rotate downward.

36
00:02:29,310 --> 00:02:31,350
‫We want it to still point forward.

37
00:02:31,350 --> 00:02:33,780
‫So this is the result that we want.

38
00:02:33,810 --> 00:02:39,180
‫In other words, we only want to change the yea component of the tank's turret rotation.

39
00:02:39,180 --> 00:02:44,550
‫So that way it's pointing in the correct direction but still remains parallel to the ground.

40
00:02:44,550 --> 00:02:51,060
‫We've only changed its yaw, so let's hop back into VS code and create a function for rotating the tank

41
00:02:51,060 --> 00:02:51,600
‫turret.

42
00:02:51,630 --> 00:02:57,210
‫Now we're going to be rotating the turret on the tank, but we'd also like to rotate the turret on our

43
00:02:57,210 --> 00:03:04,290
‫tower enemy and the tower is soon going to be created derived from base pon, which means if we create

44
00:03:04,290 --> 00:03:08,820
‫a rotate turret function we can place it here in the base pong class.

45
00:03:08,820 --> 00:03:12,930
‫So that way both the tank and the tower will inherit this function.

46
00:03:12,930 --> 00:03:18,480
‫Since we plan on using it in both the tower and the tank, we can make this function protected.

47
00:03:18,720 --> 00:03:24,630
‫So I'm going to add a protected section and any functions and variables we place in the protected section

48
00:03:24,630 --> 00:03:29,220
‫will be accessible in these child classes, but not in any other classes.

49
00:03:29,220 --> 00:03:32,580
‫So it's almost as restricted as private variables.

50
00:03:32,580 --> 00:03:34,860
‫Now this function can return void.

51
00:03:34,860 --> 00:03:37,710
‫I'm going to call this rotate turret.

52
00:03:38,850 --> 00:03:44,690
‫And in order to rotate the turret, I'm going to need a sort of destination to look at.

53
00:03:44,700 --> 00:03:49,620
‫This can be a f vector and we can call this look at target.

54
00:03:49,620 --> 00:03:56,490
‫So the idea is if we have a function that we can pass in some sort of location in the world, then the

55
00:03:56,490 --> 00:04:00,360
‫function should result in rotating the turret towards that location.

56
00:04:00,360 --> 00:04:04,680
‫So let's go ahead and provide a function definition for rotate turret.

57
00:04:04,680 --> 00:04:11,280
‫So here in base pawn CP just under the constructor, I'm going to implement rotate turret.

58
00:04:11,280 --> 00:04:16,680
‫And of course I need the a base pawn class name with the scope resolution operator here.

59
00:04:16,680 --> 00:04:21,180
‫So now that I have rotate turret, let's think about how we're going to implement this.

60
00:04:21,180 --> 00:04:28,320
‫The first thing we need is that to target vector the vector from the turret component to the trace hit

61
00:04:28,320 --> 00:04:30,120
‫result under the mouse cursor.

62
00:04:30,120 --> 00:04:38,070
‫So this can be a local F vector called to target and we're going to set it equal to the result of some

63
00:04:38,070 --> 00:04:38,970
‫vector math.

64
00:04:38,970 --> 00:04:43,350
‫Remember, it's going to be the destination minus the starting point.

65
00:04:43,350 --> 00:04:50,910
‫And the destination is look at target and the starting point is going to be the location of the tank's

66
00:04:50,910 --> 00:04:54,030
‫turret, in other words, the location of turret mesh.

67
00:04:54,030 --> 00:04:59,880
‫So we can say turret mesh and we can get the turret mesh location.

68
00:04:59,880 --> 00:05:07,290
‫Now the way to get the world location of a component is with this function get component location.

69
00:05:07,290 --> 00:05:09,630
‫It belongs to the use scene component class.

70
00:05:09,630 --> 00:05:11,460
‫So all scene components have it.

71
00:05:11,460 --> 00:05:15,690
‫So from the turret mesh we'll call get component location.

72
00:05:16,340 --> 00:05:23,600
‫So look at target minus the turret mesh location gives us the vector from the turret mesh to the lookout

73
00:05:23,600 --> 00:05:24,290
‫target.

74
00:05:24,320 --> 00:05:30,590
‫Now, as I mentioned, we can get an EF rotator from to target using the F vectors rotation function.

75
00:05:30,590 --> 00:05:37,100
‫So we're going to make an F rotator and we can call this look at rotation and we'll set this equal to

76
00:05:37,100 --> 00:05:40,100
‫to target dot rotation.

77
00:05:40,280 --> 00:05:41,570
‫That's a function call.

78
00:05:41,570 --> 00:05:46,700
‫So now we have the rotator corresponding to this to target direction vector.

79
00:05:46,700 --> 00:05:49,550
‫But the thing is we only care about the YA.

80
00:05:49,550 --> 00:05:51,680
‫We don't want to change the pitch or role.

81
00:05:51,680 --> 00:05:57,290
‫We only want to rotate the turret around the z axis and keep it parallel with the ground.

82
00:05:57,290 --> 00:05:59,420
‫Now we can do this any number of ways.

83
00:05:59,420 --> 00:06:05,870
‫For one, we can take look at rotation, pitch and zero that out.

84
00:06:05,870 --> 00:06:08,180
‫In other words, set it equal to zero.

85
00:06:08,210 --> 00:06:14,240
‫We can also take look at rotation dot role and zero that out as well.

86
00:06:14,240 --> 00:06:21,020
‫Now after we've set pitch and roll to zero, we have the rotation we want, which is only the component.

87
00:06:21,020 --> 00:06:22,460
‫This is one way to do it.

88
00:06:22,460 --> 00:06:26,450
‫Another would be to use one of the F rotator constructor overloads.

89
00:06:26,450 --> 00:06:34,820
‫If we said f rotator with parentheses and passed in some values such as zero point F for the first and

90
00:06:34,820 --> 00:06:38,300
‫the pop up is telling me that that first input is the pitch.

91
00:06:38,300 --> 00:06:43,880
‫And then for the ya we can use to target rotation.

92
00:06:44,330 --> 00:06:44,990
‫Yeah.

93
00:06:44,990 --> 00:06:47,960
‫And then for the role we could use zero dot F.

94
00:06:47,960 --> 00:06:55,820
‫Now what this does is creates a new F rotator with zero for the pitch, zero for the role and the component

95
00:06:55,820 --> 00:06:59,420
‫of the rotator returned by two target rotation.

96
00:06:59,420 --> 00:07:05,300
‫So the value returned by this would give us the same result as what we did here with these three lines.

97
00:07:05,300 --> 00:07:11,990
‫So since this is a little bit more compact and elegant, I think I'd rather use this so we can initialize

98
00:07:11,990 --> 00:07:19,370
‫the look at rotation vector with the F rotator returned by calling the F rotator constructor like this.

99
00:07:19,370 --> 00:07:21,380
‫So I'm going to remove this line down here.

100
00:07:21,380 --> 00:07:23,360
‫And now we have look at rotation.

101
00:07:23,360 --> 00:07:29,000
‫Now the last thing to do in this function is to set the rotation for the turret mash.

102
00:07:29,000 --> 00:07:36,440
‫Now we can set the rotation for a given component using this function set world rotation again, it

103
00:07:36,440 --> 00:07:38,270
‫belongs to use scene component.

104
00:07:38,270 --> 00:07:44,300
‫And the reason we're using set word rotation rather than adding some sort of local rotation offset is

105
00:07:44,300 --> 00:07:50,420
‫because we have a rotation in world space two target is a world space direction vector.

106
00:07:50,420 --> 00:07:56,000
‫We're taking the turret mesh location in world space and subtracting the look at target location in

107
00:07:56,000 --> 00:07:56,750
‫world space.

108
00:07:56,750 --> 00:07:58,850
‫So this is a world space direction.

109
00:07:58,850 --> 00:08:02,450
‫Therefore the look at rotation is a world space rotation.

110
00:08:02,450 --> 00:08:06,140
‫So we're going to use set world rotation on the turret mesh.

111
00:08:06,290 --> 00:08:13,280
‫So we'll say turret mesh, set world rotation and we're going to pass in look at rotation.

112
00:08:13,280 --> 00:08:19,550
‫So now we have a function that can take any target in the world and turn the turret mesh to look at

113
00:08:19,550 --> 00:08:22,550
‫it, but it won't rotate the mesh downwards.

114
00:08:22,550 --> 00:08:28,100
‫It's only going to change its yaw, so it'll rotate in a way that a tank turret should rotate.

115
00:08:28,100 --> 00:08:31,100
‫So we now have a function to rotate the turret.

116
00:08:31,130 --> 00:08:36,980
‫I'm going to challenge you to call this function to set that turret rotation, call the rotate turret

117
00:08:36,980 --> 00:08:38,870
‫function in the tank class.

118
00:08:38,870 --> 00:08:40,730
‫This is a protected function.

119
00:08:40,730 --> 00:08:43,070
‫So the tank class has access to it.

120
00:08:43,070 --> 00:08:49,220
‫Now rotate turret takes an F vector, the look at target, you're going to use that impact point in

121
00:08:49,220 --> 00:08:51,410
‫the F hit result from our line trace.

122
00:08:51,410 --> 00:08:57,170
‫So I'm going to leave it up to you to find the correct place in code to call this function.

123
00:08:57,410 --> 00:09:00,920
‫So pause the video and place the rotate turret function.

124
00:09:00,920 --> 00:09:02,870
‫Call in the tank class.

125
00:09:06,000 --> 00:09:06,150
‫Okay.

126
00:09:06,300 --> 00:09:09,960
‫So we have our rotate turret function and we're ready to call it.

127
00:09:10,140 --> 00:09:18,200
‫I'm going to go into tank up and here's my tick function inside the if check for player controller ref.

128
00:09:18,210 --> 00:09:24,870
‫I have my hit result and after a get hit result under cursor has been called, the hit result will have

129
00:09:24,870 --> 00:09:25,860
‫valid data.

130
00:09:25,860 --> 00:09:30,480
‫In fact, we're using that hit result impact point and draw debug sphere.

131
00:09:30,510 --> 00:09:35,370
‫Now since I don't need to draw the debug sphere anymore, I'm going to go ahead and delete that.

132
00:09:35,370 --> 00:09:39,900
‫And instead I'm going to call our protected function rotate turret.

133
00:09:39,900 --> 00:09:45,750
‫And for the look at target, I'm going to pass in hit result dot impact point.

134
00:09:47,140 --> 00:09:53,320
‫So now every frame we're going to be rotating the turret towards that impact point from our hit result.

135
00:09:53,350 --> 00:09:59,050
‫Now before I compile this, I'm no longer using draw debug helpers, so I'm going to go ahead and remove

136
00:09:59,050 --> 00:10:00,130
‫that header file.

137
00:10:00,340 --> 00:10:00,820
‫Okay.

138
00:10:00,850 --> 00:10:02,590
‫Now I'm ready to compile.

139
00:10:02,970 --> 00:10:03,360
‫Okay.

140
00:10:03,370 --> 00:10:05,050
‫I'm back here in the editor.

141
00:10:05,080 --> 00:10:09,440
‫Let's go ahead and hit play and check it out.

142
00:10:09,460 --> 00:10:11,460
‫This is amazing.

143
00:10:11,470 --> 00:10:12,910
‫I can still roll around.

144
00:10:12,910 --> 00:10:18,910
‫I can move my mouse and the turret is rotating wherever I move my mouse cursor.

145
00:10:18,910 --> 00:10:20,350
‫So this is awesome.

146
00:10:20,600 --> 00:10:25,750
‫Okay, so before we wrap this up, I'd like to just address a couple of issues.

147
00:10:25,750 --> 00:10:28,900
‫I want this to be nice and polished and looking great.

148
00:10:28,930 --> 00:10:35,680
‫Now, the first issue happens when I move the mouse cursor close to the center of the tank and then

149
00:10:35,680 --> 00:10:36,890
‫pass through it.

150
00:10:36,910 --> 00:10:42,670
‫You'll see that at some point that rotation sort of snaps very rapidly.

151
00:10:42,670 --> 00:10:48,250
‫And if we kind of move the mouse around in a haphazard manner, well, that doesn't look too great.

152
00:10:48,250 --> 00:10:49,480
‫It's snapping.

153
00:10:49,480 --> 00:10:50,890
‫We don't have smooth movement.

154
00:10:50,890 --> 00:10:52,270
‫So that's one issue.

155
00:10:52,300 --> 00:10:59,890
‫Now, the other issue is if I click and I roll around and I go up to the edge, I'll see that my turret

156
00:10:59,890 --> 00:11:03,750
‫snaps in the other direction when I move my mouse cursor up.

157
00:11:03,760 --> 00:11:05,730
‫So what's going on there?

158
00:11:05,740 --> 00:11:06,910
‫Well, let's find out.

159
00:11:06,910 --> 00:11:10,240
‫We'll go back to VS Code and I'd like to get my debug sphere back.

160
00:11:10,240 --> 00:11:16,240
‫So back here in Tank, I'm going to go to the tick function right before rotate turret and once again

161
00:11:16,240 --> 00:11:19,240
‫I'm going to call draw debug sphere.

162
00:11:19,450 --> 00:11:26,380
‫Now for the world I'm passing in get world for the center I'm passing in hit result dot impact point

163
00:11:26,380 --> 00:11:35,350
‫for the radius I'll use 25 dot F segments can be 12, color can be F color red persistent lines can

164
00:11:35,350 --> 00:11:38,440
‫be false and lifetime can be negative one.

165
00:11:38,440 --> 00:11:40,480
‫So same thing we did before.

166
00:11:40,690 --> 00:11:42,280
‫So I'm going to save this notice.

167
00:11:42,280 --> 00:11:48,700
‫I'm not adding the header file back up there and that's because debug helpers actually is included by

168
00:11:48,700 --> 00:11:50,830
‫default, so we don't technically need it.

169
00:11:50,830 --> 00:11:56,740
‫I'm just going to save jump back in the editor, do a live coding kickoff and press play.

170
00:11:56,740 --> 00:11:58,600
‫So now I have my debug sphere.

171
00:11:58,600 --> 00:12:05,320
‫So let's go up to the edge of the world and I'm going to move that mouse cursor up to where it falls

172
00:12:05,320 --> 00:12:07,240
‫off into the abyss.

173
00:12:07,240 --> 00:12:10,330
‫And that is where we have the problem.

174
00:12:10,330 --> 00:12:15,940
‫You see, we don't have a hit result under the cursor when our cursor moves up there because there's

175
00:12:15,940 --> 00:12:18,730
‫nothing there for us to get a hit result from.

176
00:12:18,730 --> 00:12:22,120
‫Therefore, our impact point is not defined.

177
00:12:22,120 --> 00:12:25,570
‫So what we need is something for that line trace to hit.

178
00:12:25,600 --> 00:12:28,810
‫Now, Unreal Engine has lots of tools that we can use.

179
00:12:28,810 --> 00:12:35,170
‫What I'm going to do is click the dropdown up here and open my place actor's panel and select this icon

180
00:12:35,170 --> 00:12:38,380
‫that says volumes and get a blocking volume.

181
00:12:38,380 --> 00:12:39,820
‫Here's blocking volume.

182
00:12:39,820 --> 00:12:41,710
‫I'm going to drag one right on in.

183
00:12:41,740 --> 00:12:44,020
‫So now I have this blocking volume.

184
00:12:44,110 --> 00:12:48,190
‫Now by default, blocking volumes are supposed to be invisible.

185
00:12:48,190 --> 00:12:54,130
‫If I go to the details panel for this and scroll down to collision, I see that it's Collision Presets

186
00:12:54,130 --> 00:13:00,940
‫is Invisible Wall, which seems to suggest that it should block almost everything.

187
00:13:00,940 --> 00:13:06,610
‫The only thing it's not blocking is the visibility channel and our line trace happens to be tracing

188
00:13:06,610 --> 00:13:08,680
‫against the visibility channel.

189
00:13:08,680 --> 00:13:13,630
‫Anything in our world that blocks the visibility channel will be hit by our line trace.

190
00:13:13,630 --> 00:13:16,540
‫But this particular volume will not.

191
00:13:16,540 --> 00:13:17,860
‫It's supposed to be invisible.

192
00:13:17,860 --> 00:13:22,690
‫Well, we can go to the collision presets, open the dropdown and select custom.

193
00:13:22,720 --> 00:13:28,300
‫This will allow us to change that so I can set the collision response to the visibility channel.

194
00:13:28,300 --> 00:13:29,170
‫It's a block.

195
00:13:29,170 --> 00:13:34,960
‫So now I have an invisible volume, but it's set to block the visibility channel, which means line

196
00:13:34,960 --> 00:13:40,180
‫traces that trace against the visibility channel like ours are going to hit it.

197
00:13:40,180 --> 00:13:45,580
‫So what I can do is I can take this blocking volume and scale it up.

198
00:13:45,580 --> 00:13:47,800
‫I'll set it just beyond the wall.

199
00:13:47,800 --> 00:13:53,440
‫In fact, I can place it right here at the bounds of the wall, and I'll bring it down a bit so that

200
00:13:53,440 --> 00:13:55,900
‫it's sort of overlapping with the wall.

201
00:13:55,900 --> 00:14:01,930
‫And that way we have this invisible barrier here, and I'm going to go ahead and scale it up in the

202
00:14:01,930 --> 00:14:03,550
‫Y direction as well.

203
00:14:03,550 --> 00:14:09,880
‫So I'll get that nice and big and position it in the middle here and give it a little bit of padding.

204
00:14:09,880 --> 00:14:14,050
‫And I'm also going to scale it up in the Z direction.

205
00:14:14,050 --> 00:14:18,460
‫So now it's nice and big and our line trace should hit it.

206
00:14:18,460 --> 00:14:20,080
‫So let's test that theory.

207
00:14:20,110 --> 00:14:23,170
‫Let's click in the viewport and go up to the wall.

208
00:14:23,170 --> 00:14:30,250
‫And sure enough, I am hitting that blocking volume with my line trace and I don't have that issue where

209
00:14:30,250 --> 00:14:34,240
‫my turret snaps to look at some undefined location.

210
00:14:34,240 --> 00:14:40,810
‫It's probably looking towards the origin of the world because that impact point vector is all zeros.

211
00:14:40,810 --> 00:14:43,660
‫Now if I go up to this wall, I have the same issue.

212
00:14:43,660 --> 00:14:46,270
‫So we need these blocking volumes.

213
00:14:46,600 --> 00:14:48,950
‫All around the perimeter of our level.

214
00:14:48,970 --> 00:14:50,590
‫Now, that can be pretty easy.

215
00:14:50,590 --> 00:14:54,820
‫All I need to do is duplicate this blocking volume.

216
00:14:54,820 --> 00:14:59,290
‫Now I can change from perspective view to top orthographic view.

217
00:14:59,290 --> 00:15:06,070
‫So I'm going to select top and scroll out with my mouse wheel and simply hold alt and duplicate this

218
00:15:06,070 --> 00:15:11,620
‫wall and place one over here and I can hit E to go into rotate mode.

219
00:15:11,620 --> 00:15:14,740
‫So I'm going to hold alt and rotate by 90 degrees.

220
00:15:14,740 --> 00:15:21,010
‫That will give me a copy and then I'll hit double u and get my translation mode going and I'll move

221
00:15:21,010 --> 00:15:26,920
‫it up here to this side and hold Alt and drag a copy down to that side.

222
00:15:26,920 --> 00:15:28,720
‫So now we have four walls.

223
00:15:28,720 --> 00:15:34,990
‫Now I can go from top view back to perspective and press play, and now I can go up to any of these

224
00:15:34,990 --> 00:15:40,240
‫walls and it doesn't matter which wall I'm covered, so that looks great.

225
00:15:40,240 --> 00:15:42,340
‫So that takes care of one of our problems.

226
00:15:42,340 --> 00:15:43,720
‫But what about this other one?

227
00:15:43,720 --> 00:15:49,360
‫If we move the mouse around close to our tank, we still have this sporadic behavior.

228
00:15:49,360 --> 00:15:51,880
‫So I'd like to take care of that as well.

229
00:15:51,880 --> 00:15:56,770
‫Now, the way I can take care of this is by interpolating that rotation.

230
00:15:56,770 --> 00:16:02,650
‫So instead of setting this rotation value directly, we can use an interpolation function which will

231
00:16:02,650 --> 00:16:08,740
‫essentially move that rotation towards the target that we provide in a smooth manner.

232
00:16:08,740 --> 00:16:10,270
‫So let's see how that works.

233
00:16:10,270 --> 00:16:15,970
‫Now, I'd like this interpolation to be in our rotate turret function so we can go back to base pawn

234
00:16:15,970 --> 00:16:22,750
‫down to rotate turret, and instead of setting our world rotation for the turret to look at rotation,

235
00:16:22,750 --> 00:16:28,240
‫we can set it equal to the value returned by an interpolation function.

236
00:16:28,240 --> 00:16:34,720
‫So I'm going to delete, look at rotation and instead we are going to call a function on the F math

237
00:16:34,720 --> 00:16:38,590
‫library called R inter two.

238
00:16:39,100 --> 00:16:44,530
‫Now our in two is designed to handle interpolation for rotators.

239
00:16:44,530 --> 00:16:49,720
‫Now its first input parameter is a const F rotator reference called current.

240
00:16:49,720 --> 00:16:53,650
‫That's the current value of the thing that we're trying to change.

241
00:16:53,650 --> 00:16:59,560
‫The thing we're trying to change is our turrets rotation and we can get that from our turret mesh.

242
00:16:59,560 --> 00:17:05,920
‫So we'll say turret mesh and the function we want to use is get component rotation.

243
00:17:05,920 --> 00:17:10,420
‫So it's a lot like get component location only it's getting the rotation.

244
00:17:10,420 --> 00:17:14,200
‫Now the next input is an F rotator called Target.

245
00:17:14,200 --> 00:17:15,310
‫That's the value.

246
00:17:15,310 --> 00:17:21,430
‫We want this turret mesh rotation to interpret to to smoothly approach.

247
00:17:21,430 --> 00:17:23,890
‫That's going to be our look at rotation.

248
00:17:23,890 --> 00:17:26,620
‫So I'm going to say look at rotation.

249
00:17:26,620 --> 00:17:28,630
‫The next input is Delta time.

250
00:17:28,630 --> 00:17:30,640
‫So we know the importance of Delta time.

251
00:17:30,640 --> 00:17:37,030
‫If we're moving things or rotating things, we want that movement to be independent of the frame rate.

252
00:17:37,030 --> 00:17:39,700
‫So for that, we can get Delta time.

253
00:17:39,700 --> 00:17:46,210
‫I can simply include gameplay statics and if you have a good memory, you'll remember that the include

254
00:17:46,210 --> 00:17:49,150
‫is kismet slash gameplay statics.

255
00:17:49,150 --> 00:17:57,700
‫So I'll get gameplay statics in here so I can access the static function, get world delta seconds and

256
00:17:57,700 --> 00:18:01,150
‫remember this requires a world context object.

257
00:18:01,150 --> 00:18:04,030
‫So I'm going to pass this into that.

258
00:18:04,030 --> 00:18:06,340
‫And last, we need an internal speed.

259
00:18:06,340 --> 00:18:12,280
‫The higher the value of internal speed, the faster we will interpolate towards the target.

260
00:18:12,280 --> 00:18:18,550
‫So if I set this to a value of say, five F, then we'll interpolate fairly slowly.

261
00:18:18,580 --> 00:18:20,470
‫Let's see how this looks now.

262
00:18:20,470 --> 00:18:25,030
‫All of this is on a single line and it runs off the screen.

263
00:18:25,030 --> 00:18:32,440
‫So what I'm going to do is place the input to set world rotation on its own line like this.

264
00:18:32,440 --> 00:18:34,060
‫So just like that.

265
00:18:34,060 --> 00:18:39,820
‫And then our interpreter, who has these four input parameters also running off the screen.

266
00:18:39,820 --> 00:18:43,270
‫So I'm going to set them each on their own lines.

267
00:18:43,270 --> 00:18:49,420
‫That way we can see all the inputs without having to scroll off the screen like that.

268
00:18:49,420 --> 00:18:49,840
‫All right.

269
00:18:49,840 --> 00:18:54,220
‫I'm going to go ahead and save that and kick off live coding.

270
00:18:54,220 --> 00:19:01,150
‫And now if I press play and I move the mouse, notice that it's taking a while for that rotation to

271
00:19:01,150 --> 00:19:01,480
‫happen.

272
00:19:01,480 --> 00:19:06,430
‫That's because we're smoothly interpolating towards that target rotation.

273
00:19:06,430 --> 00:19:07,780
‫Now, this is great.

274
00:19:07,780 --> 00:19:09,340
‫That's exactly what I wanted.

275
00:19:09,340 --> 00:19:14,950
‫No matter how I move my mouse, I'm never going to get that snappy jerky, sporadic movement.

276
00:19:15,040 --> 00:19:20,170
‫Now, five is an awfully slow Internet speed, but we can change that.

277
00:19:20,170 --> 00:19:21,820
‫We can make it a little bit faster.

278
00:19:21,820 --> 00:19:28,420
‫I'm going to set this to 25, see how that looks, and I'll let you go ahead and decide on the value

279
00:19:28,420 --> 00:19:30,040
‫that you like for your game.

280
00:19:30,040 --> 00:19:34,450
‫So one more time, kicking off live coding and pressing play.

281
00:19:34,510 --> 00:19:39,010
‫Now I have a pretty fast interpolation, but it's not snapping.

282
00:19:39,010 --> 00:19:42,910
‫It's still quick, but it's not an instantaneous snap.

283
00:19:42,910 --> 00:19:46,360
‫So as long as we're not doing something like this, well, we won't get that.

284
00:19:46,540 --> 00:19:47,860
‫Crazy behavior.

285
00:19:47,860 --> 00:19:53,500
‫And of course, if you want a slower rotation, you know how to do it now and we're done with draw debug

286
00:19:53,500 --> 00:19:56,220
‫sphere, so I'll go ahead and remove that.

287
00:19:56,230 --> 00:19:56,650
‫Okay.

288
00:19:56,650 --> 00:20:02,920
‫So in summary, we've set the turret rotation and we're passing in a look at Target, which is an F

289
00:20:02,920 --> 00:20:05,800
‫vector corresponding to a point in the world.

290
00:20:05,800 --> 00:20:08,080
‫Right now we're just using the trace hit result.

291
00:20:08,110 --> 00:20:13,090
‫Now we put this function in the base pong class and made it protected, which means that we were able

292
00:20:13,090 --> 00:20:15,880
‫to inherit the function in the tank class.

293
00:20:15,880 --> 00:20:21,190
‫And once we're ready to create a tower class based on base --, then that tower class is going to

294
00:20:21,190 --> 00:20:24,610
‫inherit this rotate turret function and we can reuse it.

295
00:20:24,610 --> 00:20:26,050
‫All we need to do is pass.

296
00:20:26,050 --> 00:20:31,750
‫In a new look at Target, we'll work on creating a tower class derived from base -- in the lectures

297
00:20:31,750 --> 00:20:32,380
‫to come.

298
00:20:32,380 --> 00:20:33,250
‫I'll see you soon.

