In this lecture we're setting up so that when we run, we change to our running animation state, and
then when we stop and idle, we're back into our idling animation state.
Just a reminder, in case you've forgotten, the animated window is where we can see the animator information.
And if we click on play, you can see that the character is probably just going to be idling, I think.
Idle, idle, idle.
Yeah.
They see the idle is going backwards and forward as the animation loops through and that character is
just idling on the spot we set up before and is running bool that if it's true then see our character
is running and then if is running is false then goes back to idling.
So that's the essence of what we're trying to do in our code.
We want to say if there's a particular condition met, then turn this ball to be true.
What is that condition?
Well, if we're running, of course, and that's going to come from whether we're pushing the controller
or the keyboard, right and left.
So over in our code, the very first thing we need to do is set up a reference for our animator or cash
reference the animator.
So little micro challenge for you here.
Pause a video and cache a reference to the animator.
Underneath the mirror your body.
I'm going to create a variable which is of type animator and I'll call this my animator using the my
convention here, my animator, my rigid body, etc. Now within start I'm going to copy the previous
line.
I don't know if that's going to save me a lot, but we'll see.
Instead of my rigid body, it's going to be my animator.
If I can spell animator, get component is going to be animator.
So there we go.
We've cached a reference, the animator that we can use in any place throughout this particular class.
Now, where's the logical spot for us to have the animation play for running?
Well, it's in the run method, so give ourselves a little bit of space in here.
We'll start by doing this in a simple way and then we'll improve it after that.
So the simple way my animator dot set bool, that's how we can change the ball from true to false.
And in parentheses, the first part of this is the name of the ball.
So I'm going to jump back over into my animator just click on is running a couple of times copy that.
So I get the exact spelling this string references.
We don't really like string references, but sometimes we use them just because we got two and then
comma and we'll say true that will set our animation to be is running.
Let's see if that works.
First of all, click on play and instead of idling you can see our character is running as evidenced
by our animation looping through the running state.
Excellent.
So that's the first part of what we wanted to do.
But we need to set something up so that we're only running if we're actually moving.
The challenge is to finish the run state.
So make the is running ball true if we're running and false if we're not and if that's enough for you
to go on, if you're ready to dive into that challenge, take that on.
Now, if you want a hint, I'll give you a hint.
And that hint is going to be to use the code we created for flipping our sprite.
So somewhere within flipping our sprite, we're saying if the player is actually moving, then do this
thing so you can grab a line from that previous code we created to see if our character is moving.
So pause the video, take on that challenge.
See you back in when you're done.
What is this mysterious line I speak of?
So.
Ball player has horizontal speed.
If our velocity is greater than zero or epsilon.
So I'm going to copy that pop that just above our my animator set.
Ball so we now have a ball within the run method that knows if we're moving and instead of my animated
set ball is running true instead of true in here we want this to be true if you're running false if
not so we can just use the same ball player has horizontal speed because this will resolve as true if
you're moving and false if you're not moving.
So save on that back into unity.
Click on play so you can see on the screen the character is running pretty little, skipping along when
we're running.
And also you can see the states are flipping backwards and forwards when we're running and not running
and our character Sprite is still flipping in the right kind of way.
So everything's good.
This is just the first state running we'll have climbing and probably a dire state and a couple of other
things later on.
But I just wanted to show you the fundamental mechanism of changing our animation state in our code.
Good stuff.
I'll see in the next lecture.