So now that we've created our Mecanim character controller.
We want to start copying code in and supplying that
character controller with values. So let's copy in some
values. We're gonna copying these variables here and what
these are is we've got the animator which is a
reference to the character controller. We've got a damping
time which is going to be used to set Uh
sort of a delay on the time that we actually
set this variable direction and the animation controller is
going to be delayed by .25 seconds. And that's
good for if you're doing things like pivoting that you
really want to use the previous rains value on.
And we also are going to have a speed value
and horizontal and vertical that's brought in from the control,
the control stick on their Xbox controller. So
let's copy and the rest of this. We want
to do in our start method. We want to
check if our animator component exists and we want to
grab it if so. So we're gonna do that
here and then we're going to set our animation controller.
So we've got that we've said our animation controller
now we want to supply it with some values.
So an update we're gonna just basically supply it values
for those two floats that we call direction and speed
Get this real fast. And speed, this calculation
here it's basically the same thing is just taking a
new vector too supplying it with the horizontal and vertical.
So making a vector out of that controller and
then describing the square magnitude which is just a little
faster than actually taking the full magnitude. And we
want to do is want to set those floats in
our animation controller. You want to set speed with
the speed value. You want to set direction with
this H value from the controllers. That's basically the
horizontal control stick. And I should probably make this
a lower case D just for good coding practices.
Great. So let's save this and have a look
make sure we didn't break Unity. Great no errors.
That's awesome. So one thing we want to
make sure to is that we understand that this set
float method here all it's doing is taking the speed
value here which is the value in our script.
It's applying it to the animation controller in right here.
So it's just taking values from the script and
putting them into our character controller. And those values
we can use to sort of control these parts of
the state machine. So let's give it a shot
and see if it works to make sure that our
script is on there. We want to make sure
that hopefully this animator component will just be set from
this character controller when we had to start. So
you can see right here the animator just came in
and we see that our direction damp time set.
Now let's try to actually move turn on the
Xbox controller. Oh, we fell over. So
there's a reason for this when we create it looks
like that game Quop. So the reason our characters
flailing on the ground is because you need to actually
set the capsule collider to freeze rotation in all of
the uh X, Y and Z axes. So
basically that's saying that you don't want the character to
fall over and have the rigid body affected by the
root motion of the animation. So we can have
a look at our character, you can see them
running around and as we move the control stick our character
runs backwards and forwards. As you can see we don't
have a camera so this is not so great.
One thing you can do that's gonna make life
a little bit easier is that there's actually a script
that I grabbed off the Unify wiki. And if
you go into the scripts folder, you'll see the
scene view camera follower, so you toss that on
the beta character, we can set this, so
it's only in play mode. What this is going
to do is it's going to create uh this ability
for our scene view to track our character. So
until we have a camera script in our project,
it's going to be pretty, pretty useful. So
we're gonna set the position offset, let's make sure
that actually will turn it off, and now we
can use this to position our character. So we're
just gonna do a 90 degree rotation X. So
we're looking straight down at the character, Uh oops,
that's 90° away from the character. Do 5 units away
away, and we're going to do 90° rotation.
So basically just looking down on the character and you
can use this value to sort of zoom out a bit.
And we can make it orthographic or
perspective. I think orthographic will be more useful
here. So let's have a look at what this
looks like. We'll make our gizmo is just a
little bit smaller, they're pretty huge right now.
So as we run around, we can see our
character from this top view. This will be useful
for debugging until we actually get a camera into our
game. Cool, so that's a good start.
I'm gonna go ahead and make sure that this is
only in play mode and that means that we can
sort of rotate this camera around when we're just in
our normal scene to you. I think it's a
good time right now to just have a moment to
think about why we're building this camera script cause you
might think, okay, so we've got ah locomotion
and everything, but there's like a lot of things
that aren't so great about this locomotion right now,
if you try to navigate these cubes it's extremely difficult,
uh and you notice that from the fact that
if you hold back on the control stick, it
means the same thing as running forward, which is
kind of an intuitive and it's really hard to tell
what direction you're moving. So the camera scripts and
the locomotion scripts that we're gonna be making this tutorial
are going to make a much more intuitive controller for
you to work with and make your game and feel
a lot better and a lot more playable. Right.
So now we're gonna add a camera script to our character.
So we're going to stop this for now.