Hey and welcome back to the third person camera tutorials
and Unity. In the last episode we added this
wall compensation effect where if the camera was was supposed
to be positioned behind the wall would actually move itself
onto the wall at the point where it hit.
And you can see that where this red vector is.
So the next thing you want to do is
we've got this effect where the character actually orbits around
the camera, which is pretty nice but it's kind
of confusing if you want to recenter the camera
behind the character. So we're gonna do is we're
going to add some scripts in that allow us to
do that. The first thing you want to do
is go into our input manager project settings, input.
We want to add a new axis in.
So we're gonna add this one called target. What
we're gonna do is we're going to set the gravity
to one and the sensitivity to one and the dead
dead zone to one. And what the dead zone
does is it makes it so that if you're just
pressing on the trigger a little bit, it won't
actually trigger to happen. So a small dead zones
usually desirable. The next step is you want to
set the actual axis and if you have a look
at this Xbox 3 60 diagram, you can see
the third axis is the triggers on the back of
the controller. So I'm going to set the joystick
access to the third axis. So that's good.
We got that set up now. So let's edit
our camera script to b05e able to do this
targeting behind the character effect. First thing I want
to do is you open up the camera script and
want to add in some new variables. You want
to add in a new constant called wide screen.
It's going to be this wide screen effect. you
can see in games like Zelda where you can run
the character and then recenter it and do this
sort of black bars come down on the screen and
we're going to do that using an image effect on
the camera, which is a pro only thing.
But if you don't have it's not necessary, you
don't need to do it. Targeting time is half
a second, and that's how long it takes for
these bars actually Tween down, uh and and move
back to being in their normal position. Next thing
you want to do is want to add in a
reference to the effect, which is going to be
called bars effect. And this is the image effect
right here. So we can open up this class
too. So you can never look at that.
We want to have a new way that we're gonna
be managing our camera and we want to do is
we want to have an enema manage our states.
So now we have this idea of multiple states in
our camera before we could just run around with the
camera. But now I have this idea of not
only can you run around, you can also target
So this will be the first of our mini
camera states. Yeah will represent that using an in
um and we'll have a reference to our current state
which is going to default to the behind camera state
which we've already coded. So as you can
see a little bit of a sneak preview here,
we've got the behind camera, the first person camera
the target camera which are working on right now
in the free camera. Next thing we want to
do is uh create a reference to that bars effect
You want to make sure that it's referencing the
right thing by performing a get component on our character
We're going to get component and grab this bar's
effect and we want to make sure that if someone's
using our third person camera, we want to require
them to have a bars affect component on their script
So we're gonna use this, require component line
up here. What that's gonna do is going to
make sure in Unity that we have that script on
our camera. It's now, let's go ahead and
add it. It's called bars effect. We can
just search for it here and there it is.
So you want to sign the shader, its name
The same thing bars effect. You want to
sign a texture, which is just going to be
a small It's pretty simple text. Let me see
if I can open it up real fast as you
can see here. It's literally two pixels. It's
one black pixel and one white pixel. You can
hardly see what it is. But we're just using
that to make this black bar effect. We've made
sure to set the the alpha from grayscale and we
made sure that we're doing point filtering which is different
from bilinear. And try linear. Point filtering is
what you want to use if you want your texture
to be scaled up and still keep the pixels the
same bilinear and try linear our interpretation methods and we
don't want to interpret this texture. Usually that's what
you want to do but not in this case.
Okay cool. So let's look back at our camera
We got this thing set up. Let's test
it real fast and see what happens. So we
can have this sort of bar effect. Seeing that
one pixel texture and we're just basically squeezing that texture
This is done using a post effect on the
camera in a shader that I wrote. You can
have a look at it if you want to.
Uh this lesson isn't about Schrader's so I'm not gonna
get into this or how this post effect works but
they're fairly simple. It's just a bunch of UV
math if you're familiar with you these. Okay so
we'll leave that for personal exploration. We'll put it
Okay cool. So now we've got our bars
effect, we're gonna just go ahead and set this
down to zero just because I don't really need it
right now. And let's look and see if we
get next in our script. So we're grabbing this
bar's effect and if it's an all we're going to
tell you should actually attach this component. Now we
want to do in late update when I change the
way that this method works right now we just have
this one mode. But now I want to make
a switch statement based on the camera state. So
we need some logic to determine what state we're in
And we're going to do that using if statement
we're gonna start this method out and we're going to
say determine camera state and we're going to check the
target access. So that's the access. We just
set in the input manager if you look at that
it's called target. So you just referenced that
with a string and you want to set the coverage
equal to a smooth step between the The coverage value
which is the current coverage value and wide screen which
is that value that we set to to 1.2.
And it's going to do that every target time and
smooth step is a function that just performs this interpretation
over a given set of time. So we do
that every target time and we're going to set the
target to target. Otherwise we're gonna Between that bars
affect 20. And we're going to set the camera
states behind. So now we need to do a
switch statement to actually execute the proper camera state.
So we got this year. So let's make our
first case and that'll be our behind camera state,
which is the current, the current one we have
going for us right now. Mhm. Yeah.
So if you'll notice um I did not put these
three things in the same method, and the reason
for that is you want to use the same variables
and want to perform these actions no matter what the
camera method is. Because it's really useful actually to
uh you want to compensate for walls, no matter
what the camera movement is, you always want to
be smoothing the camera. You always wanna be looking
at the character. So we're just kind of reusing
functionality here, keeps the code nice and clean and
keeps a little bit short and sweet. So let's
say this and make sure that everything still works because
technically we haven't changed any code yet. We haven't
done anything with this new camera effect. Just make
sure we can still run around. Okay, good
Everything looks to be good. Yeah. So
now we want to add in the logic for the
targeting behavior. We're just gonna add a new case
to a switch statement for a new behavior. Going
to call that camp states that target. And all
we're doing here is basically we're just setting the look
direction, which before was just it was this vector
show it to you real fast. It was
this vector right here, this green vector, this
one that's emitted from the camera and that just always
looks at the character no matter what direction he's facing
What we want to do is you want to
make that vector uh just be the characters forward vector
because that will reposition the camera behind him. So
just look direction equal to forward, set our target
position. And that's it. So we can just
save this guy and I don't even think we were
using looked over here. Yeah. We don't really
even need to set it so we can actually delete
this on a good So we're just setting the target
position and that's it. If you want to make
this even a little bit cleaner, we could probably
move this line of code down to here and to
set our target position and then just say look her
here as well. It might be shooting myself in
the foot by doing this, but we'll see.
It was always edit undo me. Okay, So
let's cross our fingers and hope this works. Hey
compile that's a good sign. And nice. We
get the behavior you want it. So now if
we hold the trigger, I'm holding the trigger here
you can see that the camera is sticking behind
the player and we can let go. And it
still has that behavior of having the character run around
Nice, nice, smooth tweens in between these
two camera moves and hopefully if you run up against
this wall here, we'll still be able to have
that same behavior of getting the camera pushed up against
the wall and it looks like it's doing it so
perfect. It's exactly we wanted. Okay, great
So that's the end of this lesson and I'll
see you in the next one.