Hello again.
In this lecture, we're setting up a follow camera so that we can drive our car around to all the different
parts of our world.
Let's get started.
Just going to roll up my game objects here.
So that's a little less cluttered looking.
Click on the main camera.
You can see if we hit W to access our move tool.
If we move around in our scene view, this is moving the location of the camera.
You can see that this is how it will look if our camera is moving around as we drive, that's exactly
what we want.
What we want to do is say wherever the car is, say the cars over here at the position 13 five.
Then we also want the camera to be able to that position at 13 five so the car stays right in the middle
of the screen.
Now, just one little caveat in the future will work with something called cinema screen, which will
give us another option for creating a flow camera.
But for now, I want to show you the way to do it with code so that you have that coding knowledge.
Okay.
So what we need to do is hop down into our assets directory, right click, create a C-sharp script
and we're going to call this flow camera with Uppercase F and Uppercase C, and now let's open that
up and we'll see.
As always, we have the default start and update methods.
All good so far.
So let's ask ourselves, first of all, where are we going to put this script and what are we going
to follow?
Where we put the script is very important in terms of saying we are moving or changing or doing something
to this particular thing.
And this instance we want to move the camera.
So it's best to put the script on the camera because we're doing we're moving that.
So that's the first thing.
So we're going to say this particular thing's position is going to move around in the world and its
position is going to move around in the world to match this particular thing's position.
So just undo those.
So in here, I'm just going to write this as a note right at the top here.
I'm going to say the this thing's position.
In other words, the camera should be the same as the car's position so that we know what we're doing
in this script.
And then I'll jump back over into Unity.
And before I forget, because often I do, I'm going to click on the camera and then just drag the follow
camera script onto the camera.
So we know it's there, the functionality is there.
Well, I know I'm not going to need start because what we're going to be doing is going to be happening
every single frame.
So I'm going to remove start.
I'm also going to remove this comment about what update does, because we know that pretty much by now.
And in update, I'm going to put some code in here.
We're going to start off by saying, what do we need to be accessing?
Well, we need to access this particular game object.
So this camera game objects transform and its position on the transform.
So to do that, we start by typing transform dot and then what are we accessing within there?
We're accessing the position so the value of the position and we want to be changing that or updating
that to be the same as the value of something else.
Well, this is where we need to create a reference so that unity knows what the heck we're talking about
and what we're pointing at.
Just a quick note on creating a reference, because we talk about creating references a lot.
If we want to access or change, all call or pretty much do anything to anything in our game other than
this game objects transform.
We need to create a reference.
So just to make that really super clear, this game object is the camera because we put this on the
camera.
So this is the game object.
The main camera is a game object and we can access the transform because we get that for free.
We don't need to go and make a reference to that.
We don't need to point to that because it knows automatically, Hey, if you're changing the position
of something, I'm pretty sure you're changing the position of me.
So that part's for free.
That's cool.
But everything else we need to create a reference to.
So in other words, we need to tell unity what the thing is that we're referring to.
If we wanted to access this camera component, if we want to access the car, we want access the house,
the rocks, anything else we need to say?
I'm referencing this.
I'm telling you, I want to access this.
So there's a bunch of ways to do this.
We'll look at one in this particular instance, and that is creating a serialized field that we can
point to in the inspector.
So let's do this together.
Let's start by creating a serialized field.
That means it's available in the inspector.
We can see it in the inspector and then it's going to be of type game object.
So we want to be accessing the car, the car game object, the whole thing and saying where is a position
and we're going to call it say thing to follow because we want the camera to follow this particular
thing.
Now if I save that, jump back over into unity, look at where we've got our camera follow script and
I'll just roll up a couple of these other components so they're not getting in the way.
Just camera.
And you can see on our follow camera script, if I saved over here, did I save?
So Unity is not happy with me because I was half way through writing a line and then I sidetracked to
tell you some theory.
So I'm just going to comment out that line that I was half way through writing and nudity I think will
calm down.
Okay, cool.
Rick.
Yes.
Now I'll let you do what you're trying to do.
Okay.
Now you see on our follow camera, we have this thing to follow and we've got a field just here.
And by clicking on the little selector next to this.
So just there we can click on.
Seen.
This will show us all the things in our scene we have access to.
And if I scroll around, I should be able to find cruisy mic drive, double click on Cruisy Mic Drive,
and now we have a reference.
So we're saying this is the thing I'm interested in.
This is now stored within the variable called thing to follow.
I could have also just dragged it from the scene drag, drag, drag, drag and dragged it over into
the box like that.
That's another way that we could have created that reference.
But now we're creating this reference to our player or to our vehicle.
We jump back over into our script and we can say, Just uncomment my transform position.
What do we want this to be?
We want the position of the camera to equal the position of the thing to follow.
And the way we write that is saying, let's first access the thing to follow.
And then what do you think comes next thing to follow?
Dot Transform.
Okay, what's the next bit on this?
Position.
And then we do semi-colon and save.
Now, I know that this is not quite going to work, but I'll show you what's going to happen here.
When I click on play, the camera position will be exactly the same as the card position.
So we'll click on play.
Look what happens over here in our thing.
It goes green.
And that's a case of what?
Why is it doing that?
Well, while we're still in play mode, I'll click on the 2D button over in my scene view, scroll around,
see if I can find my camera.
So double click on the camera and you can see it's literally exactly the same place as the player.
But that's no good because it means it's right on top of it.
We need some distance.
We need to be able to push it forward or push it backwards.
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
the car and I'm right on top of the car.
So we need to give ourselves a little offset.
And you can see when we're not in play mode that the camera is back a little bit.
And it doesn't matter how close or how far when we're in this 2D view, it's going to keep exactly the
same position.
But if we go too close point, then all we're going to see is the the background and we're not actually
going to see our scene.
So what do we need to do?
Well, we need to say follow the position of the car.
Exactly.
But stay back a little bit on what's this over here on the Z axis.
So move back a little bit on the Z axis.
The way we do that is just by saying go to the exact position of the car, plus a little bit extra on
Z or in this case, you can see it's minus that's changing as I push it further away from the car,
it's the minus direction.
So when you say minus, I'm just going to say ten for now because that seems an okay spot.
So just go to the car minus ten on the Z axis.
How do we do that?
Well, we've got our formula here.
And if we mouse over position, we can see that position is a vector three.
So we can't just get there and say plus minus ten or in this case it would be minus ten.
It's like, I don't know what on earth you're talking about here, Rick.
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.
It's also saying, what?
What are these numbers?
You're going to help me out here.
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
of vector three is into our current vector three.
And to do that, we need to use the new keyword and then type vector three.
So that unity knows.
Oh, okay.
You want me to add something that's a vector three to this.
What do you want to add?
And in here we can say zero, comma, zero, comma minus ten.
And of course, those clever cookies of you would realize that we can also say minus plus ten.
But for me, because I'm moving the camera minus ten on the Z axis, it makes sense to leave the minus
ten just in here.
And normally I'd say don't have any numbers right down in the middle of your code because if you need
to change something, it's going to take a while to find this number and know what's going on.
But I'm very, very confident that we're never, ever going to change this number just here.
It's fixed, it's done.
So I'm going to leave it forever.
So I'm okay leaving this down in the middle of the code and forgetting about it.
But like I say, if this was information that you might want to change at some point, then we'd need
to create a variable and store that as a variable and do that a slightly different way.
But we're not going to do that for the moment because we don't need to.
So let's go have a look at what's going on in our game here.
I've still left everything in 3D mode in the scene so we can see what's happening.
Click on play, drive around and you can see that we're being followed around and we've got our nice
distance of minus ten on the Z axis.
We're staying away from the car, so it's perfect.
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
do in here.
You might not be seeing any issues just yet, but we could get some issues with our camera being jittery,
and we probably will in the future as we add more stuff in here.
And that's because we will get a little bit of a conflict where the game says, I'm going to move the
camera and I'm going to move the car.
But sometimes I'll move the camera a little bit slower than the car, a little bit earlier than the
car, because we haven't said specifically exactly when it should happen.
So this brings me to an opportunity to talk to you about the unity execution order.
And what that is, is all of the things that unity does when we push play.
And then on every single frame that our game is running, there's a lot going on, a lot we don't need
to know about.
That's happening behind the scenes.
But we know we've already played around with the START method or the START function when we click play,
this is part of the initialization.
In other words, starting things happening.
There's also another method we could be calling called a wake.
This is another callback that happens even before start.
So if you need something to happen in the very instance, the very, very first thing that happens awake
is going to be better than start.
But generally START is okay for us and then we have our physics loop.
So again, you don't need to understand all the aspects of this, but just know that physics is being
calculated at a different rate to a lot of the other things in our game, such as our game logic.
Physics is fixed and the reason it's fixed is so that there can be predictability with physics.
If physics was calculated too much, it might be too intensive for the game.
So physics is often requires a lot of calculations.
So within unity, physics runs with its own loop and update can be a little bit different sometimes.
Okay.
And then what we also see within the general update.
So here's our update here.
This is happening on every frame and we get more.
Frames on a faster computer and less frames on a slower computer, but it's still running through update
and we haven't hear something called Late Update, which means with this whole update cycle, all the
things that are happening on update, each frame we can say actually not do this as the very last thing
that happens in the game logic, the very last update thing.
And then there's a few other things in here that we'll explore throughout this course.
But for the moment, we don't need to look at all these various things just to get the concept.
There's a lot of things going on and they can happen at slightly different times.
So if we jump back in here instead of placing our camera movement in update, if we place it in late
update, which is just a case of popping in late in front of update, and that will change this whole
thing from update to late update.
Now we can guarantee that our camera will absolutely follow our car smoothly because we know it's not
going to be fighting with the car in terms of what's my position and where are you and where am I?
Let's just check.
This works.
I'll look a little bit silly if it doesn't work.
When I click on play I can as we drive around.
That should be nice.
Unless I bump into a tree.
Should be nice and smooth because we now have things in late update.
Okay, cool.
So that's what I wanted to show you.
I wanted to get the camera working and just have a little bit of a conversation about the unity execution
order, specifically that we need to put our camera in late update so we don't have the jitteriness.
Great work.
I'll see you in the next lecture.