Okay, welcome back. So so far we've got
our character controller set up with basic movement with our
Xbox controller so we can run around and that's about
it but we don't have a camera yet so it's
really hard to tell where you're going. So we're
gonna do is we're gonna add a really basic follow
camera and we're just gonna follow at a fixed
distance from the character. So we just created an
empty script here. Opened it up in MonoDevelop,
we're gonna copy in some code. So the
first thing I wanna do is add in some variables
and for those variables are gonna have a distance away
which is going to be a fixed distance from the
back of the character where we want to position the
camera a fixed distance up from the character. We
want to set the camera a smooth value which is
going to be how long it takes from the camera
to move from its current position where we're trying to
place the camera and we're going to have a follow
which is going to be a transform. The reason
we're doing this is because if you actually follow just
the characters transform since the character has a rigid body
attached to it, his position is going to be
updated with the physics calculations, which happens in fixed
update, which is bad because his, his position
doesn't get updated as much, so it'll be really
jerky. So what we did is we just created
a really um just an empty game object, position
it at zero as a child of the beta character
we're going to follow that instead, we just
tagged that guy a player. So tag him player and
we'll go back to the script and we've got our
target position, which is where we're trying to position
the camera. We're going to take smooth time to
get there. So let's add in a call to
find game object using the tagged player that we added
. We're going to set that up to follow.
So it's going to just quickly grab this transform right
here and just toss into our script. And now
we're going to copy some code into another Unity function
called lateupdates. This is the Unity event function.
That is useful for camera stuff because after you've
positioned all your objects, you can put your camera
code here and you'll be sure that since this happens
after update that everything will have been positioned by this
point. It's copy this code, place it in
and let's have a look at what we're doing here.
So we're setting target position. What we're doing
is we're taking the follow position. We're moving up
by doing vector edition by this distance. And moving
back by distance away by using subtraction based on the
follow of the character, his forward vector. So
we've got our character here. If you look at
the character his follow transform, his foward transforms this
blue error here, his Z direction and his up arrow.
Is this uh this direction right here. This
Y value. So we have a look at
that script again and uh we're just using vector3.up
and uh vector3 and follow.forward.
So we could just make this follow.up,
just sort of making sure that we're always getting
the characters follow in different scenarios. Vectors3.up
is 010 And characters.up we'll always be 010 since
we're just on a 2-D plane. But
just in case he moves around, we want to
make sure we take that into account. What we've
done here is we've drawn three rays just as a
debug were drawn the position the characters position to this
up vector that we've created, this up offset we've drawn
the characters position to this away distance offset that we've
created. We've drawn one more which is a line
from the following position to the target position. Let's
have a look at what those look like in the
editor. So let's save this script. Go back
to the Unity editor and hit play and let's look
at those vectors that we just drew. Mhm doesn't
look like it's updating and that is because we did
not set any scripture variables for the camera. So
we need to actually set those distance away. We're
going to set to be, We can go with
five, maybe distance up as 2000 m. And
we're going to take uh we use three is our
smoothing value. And just to make sure you realize
what that's for. If you look in mono develop
, we've got time dot delta time time smooth.
So what this is doing with this alert is it's
normalizing it using time delta time, which is the
difference in time between frames. So that's sort of
like a platform agnostic way of making sure that your
values and realize, let's give us one more shot
. Alright, great. Now we can see our
values. So we've got this blue vector here,
which is our distance away and this red vector which
is our distance up. And you can see if
you add those two vectors, you get this vector
, which is I I mean you can do it
using pythagorean theorem and that's where you want to place
the camera. So that's what all of this line
of code is doing right here, seeing all this
math right here. So that's nice. But now
we actually want to do code to position the camera
there. So we take the transform of the of
the, sorry position the camera there. So you
want to take the position of the camera and set
it equal to the lower part of its current position
. And we're literally interpreting between the current position and
this purple vector where we want to place the camera
And we're doing that over over this much 0-1 based
on this time. So as time progresses it will
move into that position. And the last thing I
want to do is make sure that we look at
, we're making sure the camera faces towards the character
. That's what transformed out look at. Does it
just make sure that the camera's always facing the character
? Let's run and see if it worked awesome.
You can sell, it works pretty well. It's
a really basic camera, it's not great but it
follows behind the character and it's easy enough to move
through the blocks. Now. Let's have a look
at that. Using our little debug right here.
Yeah. Okay, so as you can see those
three vectors, you can see how the cameras being
positioned based off that. And if you look at
the actual camera objects, you know, it's like
swinging back and forth trying to position at that purple
vector. That's the result of this smoothing value.
So if you increase the smoothing value of this camera
will be more snappy. So this is like 10
or Just just to prove the point, maybe 50
As you can see now, it definitely sticks much
closer to that vector and it's almost 1-1 now.
So that's it for the first version of the camera
as you can see, it's not great. We're
going to be improving on that in the next lesson