1
00:00:03,030 --> 00:00:03,540
Hello again.

2
00:00:03,540 --> 00:00:08,310
In this lecture, we're setting up a follow camera so that we can drive our car around to all the different

3
00:00:08,310 --> 00:00:09,600
parts of our world.

4
00:00:09,600 --> 00:00:10,800
Let's get started.

5
00:00:10,830 --> 00:00:12,810
Just going to roll up my game objects here.

6
00:00:12,810 --> 00:00:14,190
So that's a little less cluttered looking.

7
00:00:14,190 --> 00:00:15,450
Click on the main camera.

8
00:00:15,450 --> 00:00:19,290
You can see if we hit W to access our move tool.

9
00:00:19,290 --> 00:00:23,610
If we move around in our scene view, this is moving the location of the camera.

10
00:00:23,610 --> 00:00:28,380
You can see that this is how it will look if our camera is moving around as we drive, that's exactly

11
00:00:28,410 --> 00:00:28,890
what we want.

12
00:00:28,890 --> 00:00:36,030
What we want to do is say wherever the car is, say the cars over here at the position 13 five.

13
00:00:36,030 --> 00:00:42,150
Then we also want the camera to be able to that position at 13 five so the car stays right in the middle

14
00:00:42,150 --> 00:00:42,990
of the screen.

15
00:00:42,990 --> 00:00:47,190
Now, just one little caveat in the future will work with something called cinema screen, which will

16
00:00:47,190 --> 00:00:49,830
give us another option for creating a flow camera.

17
00:00:49,830 --> 00:00:53,970
But for now, I want to show you the way to do it with code so that you have that coding knowledge.

18
00:00:53,970 --> 00:00:54,210
Okay.

19
00:00:54,210 --> 00:01:00,240
So what we need to do is hop down into our assets directory, right click, create a C-sharp script

20
00:01:00,240 --> 00:01:08,280
and we're going to call this flow camera with Uppercase F and Uppercase C, and now let's open that

21
00:01:08,280 --> 00:01:09,360
up and we'll see.

22
00:01:09,360 --> 00:01:12,510
As always, we have the default start and update methods.

23
00:01:12,510 --> 00:01:13,710
All good so far.

24
00:01:13,710 --> 00:01:18,840
So let's ask ourselves, first of all, where are we going to put this script and what are we going

25
00:01:18,840 --> 00:01:19,590
to follow?

26
00:01:19,590 --> 00:01:24,480
Where we put the script is very important in terms of saying we are moving or changing or doing something

27
00:01:24,480 --> 00:01:25,770
to this particular thing.

28
00:01:25,770 --> 00:01:28,620
And this instance we want to move the camera.

29
00:01:28,620 --> 00:01:32,070
So it's best to put the script on the camera because we're doing we're moving that.

30
00:01:32,070 --> 00:01:33,000
So that's the first thing.

31
00:01:33,000 --> 00:01:39,090
So we're going to say this particular thing's position is going to move around in the world and its

32
00:01:39,090 --> 00:01:43,140
position is going to move around in the world to match this particular thing's position.

33
00:01:43,140 --> 00:01:44,610
So just undo those.

34
00:01:44,610 --> 00:01:48,340
So in here, I'm just going to write this as a note right at the top here.

35
00:01:48,360 --> 00:01:51,870
I'm going to say the this thing's position.

36
00:01:51,870 --> 00:01:59,640
In other words, the camera should be the same as the car's position so that we know what we're doing

37
00:01:59,640 --> 00:02:00,420
in this script.

38
00:02:00,420 --> 00:02:02,280
And then I'll jump back over into Unity.

39
00:02:02,280 --> 00:02:07,260
And before I forget, because often I do, I'm going to click on the camera and then just drag the follow

40
00:02:07,260 --> 00:02:08,940
camera script onto the camera.

41
00:02:08,940 --> 00:02:11,250
So we know it's there, the functionality is there.

42
00:02:11,250 --> 00:02:15,600
Well, I know I'm not going to need start because what we're going to be doing is going to be happening

43
00:02:15,600 --> 00:02:16,650
every single frame.

44
00:02:16,650 --> 00:02:18,270
So I'm going to remove start.

45
00:02:18,270 --> 00:02:22,260
I'm also going to remove this comment about what update does, because we know that pretty much by now.

46
00:02:22,260 --> 00:02:25,770
And in update, I'm going to put some code in here.

47
00:02:26,040 --> 00:02:29,100
We're going to start off by saying, what do we need to be accessing?

48
00:02:29,100 --> 00:02:31,890
Well, we need to access this particular game object.

49
00:02:31,890 --> 00:02:36,870
So this camera game objects transform and its position on the transform.

50
00:02:36,870 --> 00:02:42,450
So to do that, we start by typing transform dot and then what are we accessing within there?

51
00:02:42,450 --> 00:02:48,000
We're accessing the position so the value of the position and we want to be changing that or updating

52
00:02:48,000 --> 00:02:51,090
that to be the same as the value of something else.

53
00:02:51,090 --> 00:02:56,760
Well, this is where we need to create a reference so that unity knows what the heck we're talking about

54
00:02:56,760 --> 00:02:57,600
and what we're pointing at.

55
00:02:57,600 --> 00:03:01,680
Just a quick note on creating a reference, because we talk about creating references a lot.

56
00:03:01,680 --> 00:03:08,520
If we want to access or change, all call or pretty much do anything to anything in our game other than

57
00:03:08,520 --> 00:03:10,260
this game objects transform.

58
00:03:10,260 --> 00:03:11,430
We need to create a reference.

59
00:03:11,430 --> 00:03:16,830
So just to make that really super clear, this game object is the camera because we put this on the

60
00:03:16,830 --> 00:03:17,550
camera.

61
00:03:17,550 --> 00:03:18,990
So this is the game object.

62
00:03:18,990 --> 00:03:24,030
The main camera is a game object and we can access the transform because we get that for free.

63
00:03:24,030 --> 00:03:25,860
We don't need to go and make a reference to that.

64
00:03:25,860 --> 00:03:29,580
We don't need to point to that because it knows automatically, Hey, if you're changing the position

65
00:03:29,580 --> 00:03:32,550
of something, I'm pretty sure you're changing the position of me.

66
00:03:32,550 --> 00:03:33,630
So that part's for free.

67
00:03:33,630 --> 00:03:34,290
That's cool.

68
00:03:34,290 --> 00:03:37,230
But everything else we need to create a reference to.

69
00:03:37,260 --> 00:03:41,400
So in other words, we need to tell unity what the thing is that we're referring to.

70
00:03:41,400 --> 00:03:47,340
If we wanted to access this camera component, if we want to access the car, we want access the house,

71
00:03:47,340 --> 00:03:49,170
the rocks, anything else we need to say?

72
00:03:49,170 --> 00:03:50,640
I'm referencing this.

73
00:03:50,640 --> 00:03:52,290
I'm telling you, I want to access this.

74
00:03:52,290 --> 00:03:54,000
So there's a bunch of ways to do this.

75
00:03:54,000 --> 00:03:59,760
We'll look at one in this particular instance, and that is creating a serialized field that we can

76
00:03:59,760 --> 00:04:01,590
point to in the inspector.

77
00:04:01,590 --> 00:04:02,730
So let's do this together.

78
00:04:02,730 --> 00:04:06,180
Let's start by creating a serialized field.

79
00:04:06,180 --> 00:04:07,800
That means it's available in the inspector.

80
00:04:07,800 --> 00:04:12,180
We can see it in the inspector and then it's going to be of type game object.

81
00:04:12,190 --> 00:04:17,640
So we want to be accessing the car, the car game object, the whole thing and saying where is a position

82
00:04:17,640 --> 00:04:22,560
and we're going to call it say thing to follow because we want the camera to follow this particular

83
00:04:22,560 --> 00:04:23,040
thing.

84
00:04:23,040 --> 00:04:28,530
Now if I save that, jump back over into unity, look at where we've got our camera follow script and

85
00:04:28,530 --> 00:04:31,350
I'll just roll up a couple of these other components so they're not getting in the way.

86
00:04:31,350 --> 00:04:32,130
Just camera.

87
00:04:32,130 --> 00:04:36,660
And you can see on our follow camera script, if I saved over here, did I save?

88
00:04:36,660 --> 00:04:41,190
So Unity is not happy with me because I was half way through writing a line and then I sidetracked to

89
00:04:41,190 --> 00:04:42,090
tell you some theory.

90
00:04:42,090 --> 00:04:46,710
So I'm just going to comment out that line that I was half way through writing and nudity I think will

91
00:04:46,710 --> 00:04:47,220
calm down.

92
00:04:47,400 --> 00:04:48,060
Okay, cool.

93
00:04:48,060 --> 00:04:48,450
Rick.

94
00:04:48,450 --> 00:04:48,810
Yes.

95
00:04:48,810 --> 00:04:51,120
Now I'll let you do what you're trying to do.

96
00:04:51,150 --> 00:04:51,420
Okay.

97
00:04:51,420 --> 00:04:56,910
Now you see on our follow camera, we have this thing to follow and we've got a field just here.

98
00:04:56,910 --> 00:05:00,300
And by clicking on the little selector next to this.

99
00:05:00,300 --> 00:05:02,400
So just there we can click on.

100
00:05:02,520 --> 00:05:03,450
Seen.

101
00:05:03,480 --> 00:05:06,270
This will show us all the things in our scene we have access to.

102
00:05:06,300 --> 00:05:11,730
And if I scroll around, I should be able to find cruisy mic drive, double click on Cruisy Mic Drive,

103
00:05:11,730 --> 00:05:13,470
and now we have a reference.

104
00:05:13,470 --> 00:05:15,950
So we're saying this is the thing I'm interested in.

105
00:05:15,960 --> 00:05:20,550
This is now stored within the variable called thing to follow.

106
00:05:20,580 --> 00:05:24,900
I could have also just dragged it from the scene drag, drag, drag, drag and dragged it over into

107
00:05:24,900 --> 00:05:25,770
the box like that.

108
00:05:25,770 --> 00:05:28,380
That's another way that we could have created that reference.

109
00:05:28,380 --> 00:05:31,620
But now we're creating this reference to our player or to our vehicle.

110
00:05:31,620 --> 00:05:37,350
We jump back over into our script and we can say, Just uncomment my transform position.

111
00:05:37,350 --> 00:05:38,250
What do we want this to be?

112
00:05:38,290 --> 00:05:43,560
We want the position of the camera to equal the position of the thing to follow.

113
00:05:43,560 --> 00:05:47,670
And the way we write that is saying, let's first access the thing to follow.

114
00:05:47,670 --> 00:05:50,490
And then what do you think comes next thing to follow?

115
00:05:50,490 --> 00:05:53,160
Dot Transform.

116
00:05:53,280 --> 00:05:54,990
Okay, what's the next bit on this?

117
00:05:55,720 --> 00:05:56,710
Position.

118
00:05:57,130 --> 00:05:59,500
And then we do semi-colon and save.

119
00:05:59,530 --> 00:06:03,490
Now, I know that this is not quite going to work, but I'll show you what's going to happen here.

120
00:06:03,490 --> 00:06:08,740
When I click on play, the camera position will be exactly the same as the card position.

121
00:06:08,740 --> 00:06:09,790
So we'll click on play.

122
00:06:09,820 --> 00:06:12,040
Look what happens over here in our thing.

123
00:06:12,070 --> 00:06:13,150
It goes green.

124
00:06:13,150 --> 00:06:14,650
And that's a case of what?

125
00:06:14,650 --> 00:06:15,430
Why is it doing that?

126
00:06:15,430 --> 00:06:20,280
Well, while we're still in play mode, I'll click on the 2D button over in my scene view, scroll around,

127
00:06:20,290 --> 00:06:21,760
see if I can find my camera.

128
00:06:21,760 --> 00:06:27,670
So double click on the camera and you can see it's literally exactly the same place as the player.

129
00:06:27,670 --> 00:06:30,280
But that's no good because it means it's right on top of it.

130
00:06:30,280 --> 00:06:31,180
We need some distance.

131
00:06:31,180 --> 00:06:33,280
We need to be able to push it forward or push it backwards.

132
00:06:33,280 --> 00:06:37,030
And it won't let me do this in play mode because it's saying, no, you told me to be right on top of

133
00:06:37,030 --> 00:06:38,560
the car and I'm right on top of the car.

134
00:06:38,560 --> 00:06:40,510
So we need to give ourselves a little offset.

135
00:06:40,510 --> 00:06:44,410
And you can see when we're not in play mode that the camera is back a little bit.

136
00:06:44,410 --> 00:06:49,120
And it doesn't matter how close or how far when we're in this 2D view, it's going to keep exactly the

137
00:06:49,120 --> 00:06:49,630
same position.

138
00:06:49,630 --> 00:06:55,540
But if we go too close point, then all we're going to see is the the background and we're not actually

139
00:06:55,540 --> 00:06:56,620
going to see our scene.

140
00:06:56,620 --> 00:06:57,580
So what do we need to do?

141
00:06:57,580 --> 00:07:00,040
Well, we need to say follow the position of the car.

142
00:07:00,040 --> 00:07:01,030
Exactly.

143
00:07:01,030 --> 00:07:05,260
But stay back a little bit on what's this over here on the Z axis.

144
00:07:05,260 --> 00:07:07,390
So move back a little bit on the Z axis.

145
00:07:07,440 --> 00:07:12,790
The way we do that is just by saying go to the exact position of the car, plus a little bit extra on

146
00:07:12,790 --> 00:07:17,290
Z or in this case, you can see it's minus that's changing as I push it further away from the car,

147
00:07:17,290 --> 00:07:18,700
it's the minus direction.

148
00:07:18,700 --> 00:07:22,870
So when you say minus, I'm just going to say ten for now because that seems an okay spot.

149
00:07:22,870 --> 00:07:26,140
So just go to the car minus ten on the Z axis.

150
00:07:26,140 --> 00:07:26,860
How do we do that?

151
00:07:26,860 --> 00:07:29,080
Well, we've got our formula here.

152
00:07:29,080 --> 00:07:32,650
And if we mouse over position, we can see that position is a vector three.

153
00:07:32,650 --> 00:07:37,150
So we can't just get there and say plus minus ten or in this case it would be minus ten.

154
00:07:37,150 --> 00:07:39,580
It's like, I don't know what on earth you're talking about here, Rick.

155
00:07:39,580 --> 00:07:47,920
And we also can't say plus, let's see, zero on the X is zero on the Y and minus ten on the Z.

156
00:07:47,920 --> 00:07:49,210
It's also saying, what?

157
00:07:49,300 --> 00:07:50,310
What are these numbers?

158
00:07:50,310 --> 00:07:51,280
You're going to help me out here.

159
00:07:51,280 --> 00:07:56,830
So there's a way we can do this by saying we want to add a vector three, which is X, Y and Z, sort

160
00:07:56,830 --> 00:07:59,740
of vector three is into our current vector three.

161
00:07:59,740 --> 00:08:04,510
And to do that, we need to use the new keyword and then type vector three.

162
00:08:04,510 --> 00:08:05,800
So that unity knows.

163
00:08:05,800 --> 00:08:06,820
Oh, okay.

164
00:08:06,820 --> 00:08:09,700
You want me to add something that's a vector three to this.

165
00:08:09,700 --> 00:08:10,990
What do you want to add?

166
00:08:10,990 --> 00:08:15,760
And in here we can say zero, comma, zero, comma minus ten.

167
00:08:15,760 --> 00:08:21,400
And of course, those clever cookies of you would realize that we can also say minus plus ten.

168
00:08:21,400 --> 00:08:26,650
But for me, because I'm moving the camera minus ten on the Z axis, it makes sense to leave the minus

169
00:08:26,650 --> 00:08:27,640
ten just in here.

170
00:08:27,640 --> 00:08:32,809
And normally I'd say don't have any numbers right down in the middle of your code because if you need

171
00:08:32,809 --> 00:08:35,950
to change something, it's going to take a while to find this number and know what's going on.

172
00:08:35,950 --> 00:08:40,690
But I'm very, very confident that we're never, ever going to change this number just here.

173
00:08:40,690 --> 00:08:41,799
It's fixed, it's done.

174
00:08:41,799 --> 00:08:42,820
So I'm going to leave it forever.

175
00:08:42,820 --> 00:08:46,240
So I'm okay leaving this down in the middle of the code and forgetting about it.

176
00:08:46,240 --> 00:08:50,230
But like I say, if this was information that you might want to change at some point, then we'd need

177
00:08:50,230 --> 00:08:54,190
to create a variable and store that as a variable and do that a slightly different way.

178
00:08:54,190 --> 00:08:56,530
But we're not going to do that for the moment because we don't need to.

179
00:08:56,560 --> 00:08:58,780
So let's go have a look at what's going on in our game here.

180
00:08:58,780 --> 00:09:02,680
I've still left everything in 3D mode in the scene so we can see what's happening.

181
00:09:02,680 --> 00:09:07,000
Click on play, drive around and you can see that we're being followed around and we've got our nice

182
00:09:07,000 --> 00:09:09,520
distance of minus ten on the Z axis.

183
00:09:09,520 --> 00:09:11,380
We're staying away from the car, so it's perfect.

184
00:09:11,410 --> 00:09:15,850
I'm just going to get out of play mode, pop that back into two D There's one last thing I'm going to

185
00:09:15,850 --> 00:09:16,270
do in here.

186
00:09:16,270 --> 00:09:22,660
You might not be seeing any issues just yet, but we could get some issues with our camera being jittery,

187
00:09:22,660 --> 00:09:25,480
and we probably will in the future as we add more stuff in here.

188
00:09:25,480 --> 00:09:31,030
And that's because we will get a little bit of a conflict where the game says, I'm going to move the

189
00:09:31,030 --> 00:09:32,950
camera and I'm going to move the car.

190
00:09:32,950 --> 00:09:36,790
But sometimes I'll move the camera a little bit slower than the car, a little bit earlier than the

191
00:09:36,790 --> 00:09:40,630
car, because we haven't said specifically exactly when it should happen.

192
00:09:40,630 --> 00:09:45,460
So this brings me to an opportunity to talk to you about the unity execution order.

193
00:09:45,460 --> 00:09:49,660
And what that is, is all of the things that unity does when we push play.

194
00:09:49,660 --> 00:09:54,130
And then on every single frame that our game is running, there's a lot going on, a lot we don't need

195
00:09:54,130 --> 00:09:54,640
to know about.

196
00:09:54,640 --> 00:09:55,990
That's happening behind the scenes.

197
00:09:55,990 --> 00:10:01,210
But we know we've already played around with the START method or the START function when we click play,

198
00:10:01,210 --> 00:10:02,890
this is part of the initialization.

199
00:10:02,890 --> 00:10:04,630
In other words, starting things happening.

200
00:10:04,630 --> 00:10:07,810
There's also another method we could be calling called a wake.

201
00:10:07,810 --> 00:10:10,540
This is another callback that happens even before start.

202
00:10:10,540 --> 00:10:15,490
So if you need something to happen in the very instance, the very, very first thing that happens awake

203
00:10:15,490 --> 00:10:16,570
is going to be better than start.

204
00:10:16,570 --> 00:10:20,290
But generally START is okay for us and then we have our physics loop.

205
00:10:20,290 --> 00:10:24,700
So again, you don't need to understand all the aspects of this, but just know that physics is being

206
00:10:24,700 --> 00:10:30,970
calculated at a different rate to a lot of the other things in our game, such as our game logic.

207
00:10:30,970 --> 00:10:36,550
Physics is fixed and the reason it's fixed is so that there can be predictability with physics.

208
00:10:36,550 --> 00:10:41,050
If physics was calculated too much, it might be too intensive for the game.

209
00:10:41,050 --> 00:10:43,630
So physics is often requires a lot of calculations.

210
00:10:43,630 --> 00:10:49,420
So within unity, physics runs with its own loop and update can be a little bit different sometimes.

211
00:10:49,420 --> 00:10:49,710
Okay.

212
00:10:49,720 --> 00:10:51,790
And then what we also see within the general update.

213
00:10:51,790 --> 00:10:52,960
So here's our update here.

214
00:10:52,960 --> 00:10:55,090
This is happening on every frame and we get more.

215
00:10:55,160 --> 00:10:59,750
Frames on a faster computer and less frames on a slower computer, but it's still running through update

216
00:10:59,750 --> 00:11:04,850
and we haven't hear something called Late Update, which means with this whole update cycle, all the

217
00:11:04,850 --> 00:11:11,000
things that are happening on update, each frame we can say actually not do this as the very last thing

218
00:11:11,000 --> 00:11:14,240
that happens in the game logic, the very last update thing.

219
00:11:14,240 --> 00:11:17,210
And then there's a few other things in here that we'll explore throughout this course.

220
00:11:17,210 --> 00:11:20,990
But for the moment, we don't need to look at all these various things just to get the concept.

221
00:11:21,000 --> 00:11:24,740
There's a lot of things going on and they can happen at slightly different times.

222
00:11:24,740 --> 00:11:30,650
So if we jump back in here instead of placing our camera movement in update, if we place it in late

223
00:11:30,650 --> 00:11:35,480
update, which is just a case of popping in late in front of update, and that will change this whole

224
00:11:35,480 --> 00:11:37,130
thing from update to late update.

225
00:11:37,130 --> 00:11:43,730
Now we can guarantee that our camera will absolutely follow our car smoothly because we know it's not

226
00:11:43,730 --> 00:11:48,200
going to be fighting with the car in terms of what's my position and where are you and where am I?

227
00:11:48,230 --> 00:11:48,710
Let's just check.

228
00:11:48,710 --> 00:11:49,250
This works.

229
00:11:49,250 --> 00:11:50,690
I'll look a little bit silly if it doesn't work.

230
00:11:50,690 --> 00:11:53,600
When I click on play I can as we drive around.

231
00:11:53,600 --> 00:11:54,410
That should be nice.

232
00:11:54,900 --> 00:11:55,970
Unless I bump into a tree.

233
00:11:55,970 --> 00:11:59,180
Should be nice and smooth because we now have things in late update.

234
00:11:59,330 --> 00:12:00,020
Okay, cool.

235
00:12:00,020 --> 00:12:00,920
So that's what I wanted to show you.

236
00:12:00,920 --> 00:12:05,060
I wanted to get the camera working and just have a little bit of a conversation about the unity execution

237
00:12:05,060 --> 00:12:10,040
order, specifically that we need to put our camera in late update so we don't have the jitteriness.

238
00:12:10,070 --> 00:12:10,640
Great work.

239
00:12:10,640 --> 00:12:11,810
I'll see you in the next lecture.

