Hi and welcome to the final third person camera tutorial
and Unity in this last tutorial, we're going to
be working on our camera and adding the last feature.
So so far we worked on the behind the
back camera, a targeting camera where it would stick
behind the character first person camera, the ability to
have collision against walls. And we've got one more
thing to work on which is the free camera and
what the free camera is is in. Uh and
a lot of the third person Nintendo games.
If you hold back on the stick, you can
free the camera and push it out and this camera
will intersect with the walls and you'll be able to
run with the character in reference to the direction of
the camera. So if I hold right Link will
run right. If I hold left, he runs
left forward backwards. Can rotate the camera while I'm
doing this. It's just an easy way to position
the camera in an exact spot. If you're trying
to do something like looking around the corner or something
like that so you can kind of look over things
and I can see those guys over there. So
that's what we'll be doing in this lesson. Open
up our script, the last tutorial for this one,
we're gonna be adding a final mode to our
camera and you can see that in our struct that
we had this one called free. So this will
be, this will be the last one we add.
And to do that we're gonna need to add
the new variables to our to our to our class.
The first thing we're gonna add is a distance
away multiplier and a distance up multiplier. And what these
are is when you're pulling back on the stick,
we need some limit to how far back you can
pull and how close up you can go. So
this is going to be basically the way that we
figure out how high you can go and how far
back you can go. The next thing we're gonna
do is we're going to add in a variable for
a threshold on the stick. So we're going to
have this threshold variable and that's going to be a
variable that says, if I pull back X amount
on the stick, that's what I want the camera
to go into this free mode. So we had
the same idea for the first person mode, but
that was actually pushing up on the stick, so
we'll go with that. The next thing I want
to add is three more variables. And these are
the minimum distance from the camera. So that's how
close you can go to the character. So that
would be that would be moving up really close probably
about that far. And we want a variable in
X and Y. So it's going to be the
minimum distance uh in terms of X. And in
terms of why height and closeness I guess are better
terms for that. And then the last variable we
want to add is actually for four more variables.
I want to have a saved rig Tegel. So
I want to save the distance that the character is
from the camera. So when we aren't holding the
stick, we know how far to position the camera
. Because we need to keep updating that on every
frame. So we want to save that distance.
The next one is distance up free in a distance
away free. And that's going to be safer to
go. You can think of as the direction that
the cameras in like the vector direction that the cameras
in and distance up and away are the actual um
Closeness in terms of height and in terms of distance
from the character. Mm And then the last one
is going to be saving the previous frames stick variables
because we wanna have it. Uh We want to
check, we want to only move the camera back
if the new direction is further. So the idea
here is if we push the camera all the way
back as soon as you push the camera back again
. Since this value is less than the previous six
value, we don't want to move the camera until
we get all the way back again, so that's
the last variable. Okay, so let's start adding
some logic at this point. First thing we want
to do is you want to add in um a
little bit. Something doesn't seem quite right but we
want to reset the target position every frame. And
the reason that we want to do this is because
in in our in our camera script, if our
target direction of our target position doesn't change like the
distance away in the distance above, then we don't
want to uh we don't if it doesn't change,
we don't want to move the move the camera from
where it's currently sitting. So we need some way
of recognizing if that target position didn't change and to
use the previous calculations for the target distance. So
this is just going to reset it every frame and
we can do a check for if target position equals
vector 3.0. Next thing we need to do is
we need to add in some logic for actually transitioning
into this case. So this year is going to
be the third person free camera case. Yeah.
And the condition here is going to be if the
right X steak is less than the threshold and if
because we're moving back on the steak And if the
speed is zero Or is rounds down to zero.
And then we also want to add a condition to
our entrance into the first person mood. And basically
just want to say that we don't want to go
into first person if we're in third person. So
that means if we got this camera mode when we
push up on the stick it shouldn't enter into first
person. So we need to we need to tell
the script that that's the case. So that's it
for that logic, let's go ahead and say the
script and see if you get in years. So
no areas. We got a lot of unused variables
which makes pretty good sense. Let's add a new
case in here and we'll just check and make sure
that we're actually registering this camera behavior. So we're
going to add a new case to a switch statement
and it's just going to say if the camera state
is in free state. Okay. The break statement
and what is that? A debug statement in there
just to just to check. Okay. Mhm.
Mhm. Hopefully this will compile and let's run it
. So all good. Everything still works. We
can still go into our free mode now. We're
in our Okay. So now we're in the free
mode which we just set target position equal director 3.0
. So this this behavior makes sense. Our cameras
just sitting at the origin and if we snapback it
stops giving that debug log. So it looks like
our conditions are good. I think that is good
enough there. Mhm. So next thing we want
to do is get back to our scripts and we
want to add actual logic for what to do when
we're in this state. We're going to start with
how we're going to move the camera. We want
to move the camera, we want to get the
direction from the camera to the character. And we
also want to reset the look. Wait. We
can't actually call reset camera if you notice that everything
. But first person we've been calling this reset camera
method, we can't do that here because reset camera
is going to change the local rotation of our camera
. And since we're going to be moving our camera
around in this third person mode, we need to
have control over the rotation. So we can't do
that. Instead, we're just going to call basically
the first line of this method and we're going to
put that here. So that's what this line means
. Next, we're going to calculate the rig to
gold direction, which is just the distance from the
character's head to our camera. So if you look
in unity, all that is is just minimize this
and get our character. So rick to go,
it's just going to be the direction between. It's
just the vector subtraction of our character and our camera
. And we're going to normalize that to make it
sure it's a direction. And we're going to set
the Y value equal to zero because we don't want
any Y rotation in that direction. We just wanted
in terms of X and Z. And then rick
to goal is almost the same thing. But we're
actually using the parent rig. So if you remember
in a previous lesson, we made this camera rig
object and it may not seem exactly intuitive what the
purpose of this is. But the idea is is
pretty simple. It's like creating a camera and that
they would make in Hollywood or something we want to
have control over. So if we've got our camera
here doesn't seem to be finish up, doesn't seem
to be working. So sorry about that. It
appears Photoshop gave up on. I'm trying to work
. So basically the idea of this camera rig is
that we want to have something, a secondary object
that we can rotate the camera within. So if
you're if you're just trying to rotate the camera,
we want to have this object, they can move
around the character. So you want this thing to
kind of position itself somewhere around the character in the
actual camera, we'll always look at the character and
then we want to have it be able to just
move in this direction. You can think of it
like kind of this ball and socket joint. I
think of the camera, this camera piece here as
this part of the camera and think of this this
just playing game object as this dolly. So the
dollar is going to rotate in a circle around our
character and then we're just gonna have the camera that
can kind of position itself. So it's pretty simple
idea. Hopefully that makes sense. And what we're
gonna do with that is we're going to figure out
the distance from the rig to the goal and this
is different from this because we're not actually finding direction
here, we're finding we're just calculating array here just
so you can see what that looks like. So
we're gonna, we're gonna have a look at what
the ray from the camera to the character looks like
. So see if you can see that. Really
? Oh it is. It's just a red ray
from wherever the camera is to the character. We're
going to be using that to figure out this distance
. So we're zeroing out the y and this this
rig to goal was going to basically give us the
distance away that we need. So let's move on
to the next step of the script. Next thing
we want to do is you want to calculate the
the amount that we want to pan in and out
. So that's going to be the idea of moving
the camera inside of the rig because we're just gonna
be moving in and out like this. We need
to figure out the logic for that. Uh huh
. So they had an out of section right here
for that, you know, and I've commented this
pretty heavily and I'll give it an explanation to just
so it makes sense. The idea here is if
the right stick is in the negative, so for
panting backwards we want to check and make sure that
that's less than the previous frame so that we know
we're moving back further those values less than it.
And we want to also make sure that it's this
we've got this threshold value here. I want to
make sure that the value is less than the threshold
. The reason for this is because if the value
is this this alert function here, If you don't
check this then for some reason it doesn't really work
with really, really small values of X. So
if you held this down just slightly it would it
would kind of make those values a little bit funky
. So the distance up want to calculate that by
taking distance up and then the distance multiplier. So
that's going to be if we've got a distance way
of four and the multipliers too. So we're gonna
move between four and two. We want to make
the distance free away basically be the same thing but
with the distance away multiplier and we want to,
we want to alert between these values based on the
intensity of how far you push back the wise stick
. The target position is then going to be as
simple as calculating the same idea we've normally done.
You take the follow transforms up vector, multiplied by
the up scaler. And we take the direction to
the goal which is what we what we calculated here
that we normalize and this is just a direction vector
to the character you and multiply that by the distance
away free. So let's have a look at what
that does uh and the same thing in in the
positive and here it's a little bit different the distance
up, we're going to be panning. We're going
to be lurking between the current distance up. So
we just take the the cameras, distance minus the
characters distance. That's as simple as taking the vector
from. I'll give you an example here, here's
the camera. We're just taking this distance, the
height from here to the skies height. So just
subtracting these two heights. So we're just taking the
distance up by doing that. We're painting between that
and the minimum distance that the height can be from
the character. And and for the free, we're
doing the same thing but with the minimum distance away
from the character, which is the X. Value
of that vector to And also we're taking the rectangles
magnitude. And if you saw that red vector before
, that gives us a convenient way of calculating the
X value. So, if you have a look
at that rick stengel magnitude is as simple as see
here. If you can see the red vector right
now we don't have target position set up correctly.
So it's not really working. But if you can
see that red vector that's shooting all over the place
, that's the distance, we need the distance and
X. It's really more like distance away, it's
not really an X or Y. So then we
just do the same thing, we just set target
position equal to the up times the direction, times
the upscale er and the away mhm vector times the
away scaler. Mhm. So now we just want
to smooth, we want to do a little bit
of a condition check just for now, we're going
to say, I want to keep all of the
camera free logic in one place just so we can
debug it better. So we're just going to sort
of quarantine it away from the rest of the could
by setting these values away from to not to not
compensate for the walls and not to submit the position
and not look at the transform if we're in the
free mode. And then the final thing I want
to do in this method is you want to save
the previous frames, right? X and Y values
so that we can make these checks properly. So
basically here we want to use our own smooth position
in our own look at. So we're gonna smooth
position between the transforms. Current position in the target
position, just like normal. We're going to look
at the character. So let's give this a shot
and see if it if it looks like anything,
if we still have no behavior. So we still
got some weirdness going on. Let's add in one
more line of code. Mhm. So if we
don't set a target position, if one of these
two methods doesn't get called, if neither one of
those if statements is true, then when we set
this to zero up here, it's still going to
be zero down here and we can just set the
target position equal to the previous, the previously set
distance away in distance free. Mm. So hopefully
now to work. Still nothing. So we're having
having a rough time of it here. See Oh
yeah, we need to store the direction of the
steak. So if if we don't move in the
X or Y. Stick at all, we just
need to save the old direction. Okay, sorry
about that delay right there. I had actually set
the camera when I was trying to demonstrate. This
is what I get for. Trying to do a
demonstration when I said that when I set the camera
hide up here, this is started causing bugs because
it no longer knew how to position the camera relative
to the rig. So you definitely want to make
sure that the camera is zeroed out. You need
to make sure it's transformed is parenthood exactly to the
parent rig in that way could works much better now
so we can zoom in, send him out,
can rotate around the character and we can move our
character relative to the position of the camera, which
is what and all the pivoting and stuff still works
. So this is exactly we want let's have one
more look at the code now that were uh not
that it's all in there because this is one of
the more complicated, more complicated methods. What's happening
here is that uh the way that we're doing this
rotating around, you can see here the parent rig
is set to rotate around the character offset. And
this rotate around method is basically it takes a point
to rotate around and a degree uh and access to
rotate about. So I want to we want to
rotate around the point of the character. So character
offset. The access we want to use is the
characters up access. We want to rotate based on
the direction of the stick. Whether it's moving right
or left, want to rotate in that direction some
degrees per second. So that's what that's doing.
And that gives you the effect of rotating that parent
rig around the character. So here, if we
zoom out so is him out. You can see
the parent rig is here. and if I hold
to the left or right parent rig is rotating around
the character inside of it is the camera which is
always facing down at the character in the direction of
the four directors, pointing at the character. Which
makes sense because we had set the look at to
be the character that's looking down here. But note
that the camera rig is not using the look at
. And again, the reason we had to use
this rig is because we couldn't we couldn't use this
rotate around on the camera because it's already having its
rotation manipulated by transform look at. So you can
manipulate the rotation twice. So we stuck it in
that empty rigged game object. So if you want
to see the breakdown of the script, the panning
in and out is handled by this section of the
script right here. This is panning in and out
. Mhm mm And this section here is for rotating
around. Mm hmm. Mhm. So that's about
it. And the idea is that if we don't
, if we don't have any change in our panning
, we just want to use the previous frames distance
away in distance free and set our target position that
way. So that's pretty much I think all there
is to this, I want to check one thing
really fast. I want to see if we can
combine this and get rid of this. If statement
, does that just make the code a little bit
more orthogonal? Yeah, So hopefully this will work
and we want to we want to get that compensate
for walls to handle this case too. Okay,
cool. So it looks like it still works.
Let's run over to a wall and make sure that
the camera is manages to push up against the wall
. So you know what if we if we just
hold forward on the stick, the character runs forward
, we're right on the stick, he runs right
, so that's what we wanted there and I can
rotate around, have the character of justice positioning and
it looks like they compensate for walls works too.
So we're all good there and that does it for
our camera rig. That one was probably the more
intense one to right. So hopefully this could make
sense. It's a little it's a little hard to
wrap your head around at first, but hopefully explanation
helps. And there's also comments as well. So
, thank you very much for tuning in for this
last episode. And from here on out we'll be
talking more about the character controller and mechanism. Thank 394 00:20:33.220 --> A:middle L:90% you.