Hi and welcome back to the Unity 3-D third
person camera tutorial series. In the last video we
set up a new behavior where the camera has the
character orbit around it which was pretty different and the
character actually rotates as we run around the camera.
We talked about translating the coordinates from the control stick.
Let me zoom out a second. We
talked about translating the control state coordinates, which you
can see in blue in this image into world space,
which is the green vector and then taking the
cross product, which is the red line here in
the play view. Using that to determine the direction
that the character needs to turn. That's that was
the last lesson. So in this lesson we're going
to see how we can polish the script up because
it's kind of got a problem. If we decided
to run towards the wall and we run into it
we can kind of see around the world. It
just looks sort of strange and it's not exactly what
we want, because you can get into this weird
situation where we're looking around walls instead, what we
want to do is sort of have the camera push
up against the wall when we run into it,
so we're going to implement that in this lesson.
Mhm. Okay, let's get started, let's go
into our script file, we're only going to need
to edit one file for this, and that's going
to be the third person camera file. So we
want to do is you want to go and uh
before this lesson, I actually remove that offset variable
that we used to have, and now I just
calculate uh the character offset just using using code,
which is quite a bit better. Um So we
want to do now is you want to make this
new method called compensate for walls. And what this
is going to do is going to move the camera
back to collide with the wall. We're going to
use a Raycast to do that. So we
got this method compensate for walls. We've got a from
and a to. We're going to draw a line
from the object to the target and you'll be able
to see that in science. So let's have a
look at that real fast. And let's add a
call to this in our method right before smooth position.
So let's see what this does. Okay,
As you can see in the b right here,
we have in our scene view a sigh in vector.
Now going from the camera to the character.
Uh, and that's the target where we're trying to
aim at, which is 1.5 m up from the
top of the character. So if we run into
a wall now, if you turn around and you
can see that vector is colliding with the wall.
And if you're looking at the code, there's a
ray cast right here where we're doing great cast from
the object to the target and stored in this wall.
Hit objects. So we just create a new
recast hit object called wall hit. And we can
draw a ray. And they were doing this red
ray here, which you can see in this view
museum in on it, you can see this red
ray where we hit the wall. So I'm just
drawing using vector dot left. Just a red vector
where it hits the wall. So you can see
we've got a correct value by taking this this direction
from this line right here where we go from the
from the target. I'm sorry, from the object
to the target, we hit the wall, we
draw back to where we hit, and now let's
move the camera to the X and Z coordinates while
maintaining the Y value of the camera. So we're
passing the camera's target in by reference and what this
means. We can actually change this value and we
don't have to actually store into a new value up
here. So if the wall hit happens, we
just move it, I we just moved the target
to the wall, point X and Z and the
Y value of the target. So we don't actually
change the Y value. And that's why the cameras
still here, as you can see, it's at
the X and Z values of this point, but
it maintained its wipe out. So that's helpful there,
and that's basically how it works. So that's
that's all there is to that, it's not quite
as robust as the one in uh and the Zelda
camera, just because you can get into situations like
this where you can actually see outside of the world
still, but it's definitely a step above what we
had before. So that's it for this lesson and
uh, tune in next time.