Next up, we are going to use code to say when our player should transition from
idling to running so that we can hold down the key.
And as we do that the player not only flips but
also goes into that running state.
So, let's make our player run.
So let's remind ourselves of what's going on with our state machine.
Here we have the default idling state and,
through a transition, we go into the running state.
And then, after a transition, we come back to the idling state.
What makes that transition happen is, when our condition of running is true.
So running is a bool that we've set up over here, so this is a bool.
It could mean a trigger or a float.
When this bool becomes true, because bools are true or false,
then it says execute this transition, because the condition is being met.
And then when the bool becomes false,
then you can see this condition is the transition of returning back to idling.
So this is the important string that we need to be referencing in our script,
to say when that is true or when that is false.
And so what do we need to do first with our player script?
We need to grab the animator, we need to get the animator component.
Cuz that sits on the player here.
That has the information about the player animated controller.
And the player animated controller has our running reference just here.
So let's jump over into out player script and start setting that up.
First of all we need to cache our reference to the animator.
So we need a type of animator.
And we will call this myAnimator similar to what we did with the RigidBody above.
And then, in the start method, same as with the RigidBody as well,
we need to say myAnimator equals GetComponent and
that is of type Animator, okay.
So, now we got that one,
I wanna just take a moment to give some headings to our areas in here.
Just so you can see the way Ben and I have been structuring our classes.
This one will get reasonably large, not huge, but reasonably large.
So, just to be very clear, where we put things, so
that you can be placing them in the same place as us, in the future.
So the star we put in our Config, sorry not Congig, Config.
Anything to do with before we start playing the game we can set
these parameters and variables to certain values so we can serialize our field.
And to have for example runSpeed here and
other parameters about how our script should operate.
Underneath that we tend to put, or we usually put state.
We don't have any state at the moment so I'm going to go ahead and create one so
that you can see in here.
This would be a variable of bool type and isalive = true.
And so I'll keep this around because we will need this not in this lecture not for
a few lectures until we get into death state.
But this is a state not at the moment the state is saying the player is alive,
but at some time we will need to turn this to false.
As we're playing, as we're running the game, we'll turn it to false and
in that case the player will be dead, isAlive won't be true.
After that, we set up our cached or cached, depending on how you want to say
it, component References, as we've just gone through that exercise there.
We've gone and set up the references for rigid body and animator.
And then following from that, I'm just gonna get rid of this standard study thing
here, we put in our messages, then methods.
Okay, so that's the general flow when we're putting things in there.
And as you're doing challenges, that'll give you a good anchor for
where you can lay your things out so it's consistent with what we're doing.
Now, back to making our player run.
If we go down to our run method we've got down here, what do we need to add in here?
Well, we need to be setting the bool when we're in this state of run,
so the player is running, therefore we want the player to do run animation,
underneath this run method here.
So if we say, myAnimator.
And have a look at what our options are in here, we'll see what we got,
if we scroll down we can see that we have a set bool, so I've set bool.
We can set our bool to now let's have a look at what it's asking for.
What it's asking for, I think the string name and then the bool value.
So the string name, let's jump back over onto here.
I know, Ben cringes, he winces and
cringes each time I type things in without copying and pasting.
So this will give him warm and fuzzy feelings.
Let's all do this together.
Let's go and copy running and then come back and do our script here and
past running between some quotation marks.
So we now have running and then it was looking for a bool value.
So either true or false?
So for the moment, I'm going to say this is true.
This isn't going to be the final solution, I'm gonna give that to you as a challenge.
But we'll set it to true and
we'll see what happens in our game when that is the case.
So we should start off, if I click play, we should start off
our default should be idling, and our default goes straight to running.
Okay, so it is immediately to running.
Which is good we've gone and told through our code, start to run.
But there's a problem here.
Because at any point in time, the player is always running.
So what do we need to do?
We need to do something before this to establish what
condition needs to be met in here for the player to be running.
So this is going to be your challenge for this lecture.
To Finish The Run State.
Feed in to our running animation a way to say true if the player is running and
false if the player is not.
Now I'm going to give you a hint.
If you don't need a hint, then jump in and solve this now.
It's one line of code.
And the hint is, use the code we created for flipping our sprite.
Basically to say that the player has horizontal speed,
or the player does not have horizontal speed, because that's what we're going to
be using to see if the player is running or not.
So there's a clue.
Use the code that we created for flipping, jumping, and
give that a shot to have the run state turn on and turn off.
Based upon whether we're running.
Welcome back. So
the line of code we're interested in is this playerHasHorizontalSpeed bool.
It is true if we are moving,
it is false if we're not moving on the horizontal axis here.
So I'm going to copy that and paste that above my Animation bool up here.
And then what do we need to do?
We need to feed in, instead of putting true or false,
we need to put in this bool of player has horizontal speed.
So this variable is either true or is false,
meaning our running state will be either set to true or set to false.
Let's save that and have a look to see if our player now runs and doesn't run.
Okay, standing around just idling, I move and I run, I move and I run.
Fantastic, it looks like, and you can even see down in the animation here,
it triggers, and it turns off the trigger when I let go, let go.
So that's the foundation for controlling our animation from our code we set up our
state machine, we make sure that we have a parameter which we can access.
And then in our code, when particular things are happening, for
example we are using the run method to give horizontal velocity,
we say that the running transition condition is met, and
that condition being met means that this state is true.
Great stuff, well done.
I will see you in the next video.