This lecture, we're going to fix the issue where the player is slowly sliding down the ladder.
When we want the player to kind of stay stationary, when they get onto the ladder.
The way we're going to do that is messing around with gravity.
So let me just show you something here.
If I was to turn the gravity scale on the player to zero and then run to the ladder up there, the play
is not going to drop at all.
There's no gravity whatsoever.
Now I've said it to zero, which means that also if we run around in the world, I'm not going to be
dropping.
So I want in code to say the gravity scale is zero when the player is standing on the ladder.
Now I know that's not how the world works.
If you climb up a ladder to get to the roof of your house, the gravity doesn't go to zero, because
if you fall off your ladder bump, you're going to fall off and hurt yourself.
But in this case, we're using that mechanism as a way to say, don't slide up and down the ladder.
So this will be our challenge to stop the player sliding, set the starting gravity on the player rigid
body to the rigid bodies current gravity.
So you'll need a way of finding out when we start our game what is the current gravity for the rigid
body?
And then to set a variable, which is to say this is the gravity when we first start our game and then
when the player is on the ladder, set the gravity to zero.
And obviously when the player is not on the ladder, then set the gravity back to what the starting
gravity is supposed to be.
So there you go.
There's a bit of a different sort of challenge.
Pause the video.
Take that on.
See back here when you're done.
We'll be here in our player movement script up at the very top.
I'm going to create myself another cached variable, which will be of type float, and that float will
call gravity scale at start.
Nice big name should make sense when we look back at this in the future and within my start method,
I need to define that as something.
So gravity scale start is going to be equal to my rigid body because this is where we find out gravity.
Just a reminder, if you're like, how do you even know to do that?
So if we go back to Rigid Body is the thing that controls gravity for the player.
For this particular game object, you can see the gravity scale is eight.
So it's rigid body 2d dot something to do with gravity.
So if we jump in here, my rigid body, which is the way that we've cached our reference to the rigid
body 2d so my rigid body dot gravity scale semicolon and that now case equals eight.
But if we go and change our gravity scale in here, then it will be updated nicely.
If we're not currently touching the ladder that we want the gravity to be the same as the starting gravity.
So I'm going to expand this out now.
My nice little neatly compartmentalized return.
We'll put this on its own new line, new area return will stay at the bottom because if you're not touching
it, we still want to return and not allow the player to climb.
But I'll add something in here which will be my rigid body.
Gravity scale is going to equal gravity scale at start.
So I just want to make sure we've got the right gravity.
So if you're not touching the ladder, then use the gravity scale that we had at the start.
However, if you are currently climbing, then we'll pop down here.
My rigid body dot gravity scale equals what equals zero.
Don't give the player any gravity and it's a flow.
We'll put f there just to be complete and thorough.
We'll save that and see if we've got our desired effect.
Gravity should be normal unless we're touching the ladder, in which case it should be zero.
Click on play jumping works.
Okay, climb the ladder works.
Okay.
And we're stopped here.
You can see if we look over in our rigid body on the player, it's zero and I'm on the ladder, I jump
off the ladder, it goes back up to eight.
Perfect.
So there we go.
We've got the right behavior that we wanted when we're on our ladder.
We can't we're not slowly drifting down.
We don't have our animation set up just yet.
So I think it might be time to do that in the next lecture.
Okay.
See you in the next lecture.