In this lecture, we're going to be making our play a drop from the sky and run left and run right when
we push on the keys on the keyboard.
So let's get started.
First question I have for you here is what do we need to access on our player to make the player move
around?
What's the core movement thing that physics applies to or allows us to push the player around?
It is the rigid body.
So well done if you guess that or yelled it at your computer out loud.
So we need to access the rigid body.
Let's start in our script by caching a reference to rigid body.
So if you know how to do that, pause a video and do that now.
And if you're thinking, What's Rick talking about?
Let's do that together.
So I need to create a variable which would be a type rigid body 2D.
Don't forget the 2D.
I often forget that also there's not a capitalization on the B, which I mess up as well at times,
and we'll name this rigid body actually rename this my rigid body so that it doesn't complain.
Sometimes it doesn't like when you just do rigid body because it thinks you're referring to something
else.
So my rigid body and semicolon now within start, what do we need to do?
We need to my rigid body equals get component.
What sort of component is it?
It's a rigid body to dx parentheses semicolon.
Okay, now we've got to read your body.
What do we need to do?
Well, we need to create a run method.
We need to say, move this way, move that way, run based upon what the player input is.
So first of all, in update, I'm going to call the method run.
I haven't created this yet, but I'll say run.
What does run mean?
Well, let's jump down right below on move.
We will create ourselves a new method void run parentheses, curly braces.
So let's start with a really simple implementation and then we'll make it a little bit cleverer.
So within here, what we're trying to do is say my rigid body velocity.
That's how we're going to be moving around our character equals what?
Well, we can just say for now equals our move input.
That's our vector to move input per semicolon, save that up and then jump back into unity.
Click on play.
What happens in here?
Well, we can go left, right, up and down.
Excellent.
But it's kind of weird.
The play is floating, which we don't want, and the play is moving kind of slow, which we don't want.
And the play is actually being out of fly up and down.
We only want the player to be able to move up and down when they're on a ladder.
Let's tidy up our run method in here to be a little bit more sophisticated.
So rather than just straight up move input, let's create ourselves a new variable which would be of
type vector to and we'll call this player velocity and that equals and we know we can't just say that
equals one and three because unity will say, I don't know what you're talking about here, one and
three.
What does that mean?
So we need to say, well, I'm going to give you two values and it will be in a vector two.
So new vector two, we cast it to a vector two and then player velocity knows what it is that it's supposed
to be receiving, what these numbers are that it's getting and putting into its vector two values.
So what I'm going to do on the x axis is move input.
That's currently a vector to remember.
I only want this to be on the x, so let's move input x to say just give me the x value and then comma.
I mean we could put move input y, we would get exactly the same behavior as we just had.
So we don't want that.
We want something different in here.
We don't want the player to be able to push up and down unless they're on a ladder.
And that will be in a different method, which we'll call climb ladder.
We don't want that in run.
So for now, I'm just going to put zero and see what happens.
Oh, and then the last step here is we need to make sure instead of move input, we need to have player
velocity as the values that our rigid body velocity is receiving.
So player velocity, save that up, back over into unity.
Click play.
Now we can't move up and down.
That's great.
Perfect.
But I can move left and I can move right now.
We've got some weird behavior.
The players are slowly floating down because we're saying zero, don't give it any velocity.
It's trying to resist gravity, which is not what we want.
So let's go back to our code.
I've got a challenge for you in here, and one is a simple challenge and one is a tricky challenge if
you're ready for it.
So the simple challenge here is add something to our script to give us control over how fast the player
runs.
Because you can see it's not a very compelling platform if you're only moving that quickly.
So that's the simple one.
And then the tricky one is trying to figure out how to make the character behave normally on the Y axis.
In other words, to respond to gravity, to not be able to fly up and down the air.
We've already removed that and to not get stuck.
So not just to be sort of stuck there floating in the middle of nowhere.
So see if you can have it.
So if you can figure out to say just behave normally, it's a tricky one, this one.
So if you don't get it, no worries.
But give it a shot.
Have a think about it, see if you can wrestle with it and find the solution.
So pause the video.
Take that on C back here when you are done.
First step, I'm going to add a new serialized field right up the very top serialize field, and this
will be of Type Float and I'll call this run speed and I'll initialize that at say ten, ten F because
it's a float and that's going to be our variable that we will then jump back down here and run and say,
move input X multiplied by run speed so that we can speed up how fast we're moving.
If we're receiving a minus one to plus one, we can then multiply it by ten.
So it'll be from minus ten to plus ten that we're adding to the player velocity or pushing the player
each frame.
Now what are we going to do about this pesky y value in our vector two?
What I'm going to do here is access the my rigid body velocity dot y and say that's it.
And what my logic here is, is saying that whatever your current velocity is on y, just keep that,
just do the same because we're adding a new velocity each frame.
We only need to be adding the x, we don't want to be touching the Y.
So instead of saying it's zero, we say just keep it as it currently is.
So let's see before I celebrate too much, I'll save that and see if that worked.
Click on a play.
But drops to the ground and left, right.
And we're running really fast, probably a little bit too fast, but we can tune that later on.
We can't move up.
We can't move down.
But the player is responding to gravity.
There we go, falling off the screen.
So there we go.
We've implemented left and right movement for our player using this code, using our run method so that
we can move left and right with our player.
I'll see in the next lecture.