1
00:00:03,150 --> 00:00:07,040
This lecture, we're going to fix the issue where the player is slowly sliding down the ladder.

2
00:00:07,050 --> 00:00:10,770
When we want the player to kind of stay stationary, when they get onto the ladder.

3
00:00:10,800 --> 00:00:13,370
The way we're going to do that is messing around with gravity.

4
00:00:13,380 --> 00:00:14,660
So let me just show you something here.

5
00:00:14,670 --> 00:00:21,240
If I was to turn the gravity scale on the player to zero and then run to the ladder up there, the play

6
00:00:21,240 --> 00:00:22,200
is not going to drop at all.

7
00:00:22,200 --> 00:00:24,040
There's no gravity whatsoever.

8
00:00:24,060 --> 00:00:28,110
Now I've said it to zero, which means that also if we run around in the world, I'm not going to be

9
00:00:28,110 --> 00:00:28,440
dropping.

10
00:00:28,440 --> 00:00:34,120
So I want in code to say the gravity scale is zero when the player is standing on the ladder.

11
00:00:34,140 --> 00:00:36,300
Now I know that's not how the world works.

12
00:00:36,300 --> 00:00:41,190
If you climb up a ladder to get to the roof of your house, the gravity doesn't go to zero, because

13
00:00:41,190 --> 00:00:44,250
if you fall off your ladder bump, you're going to fall off and hurt yourself.

14
00:00:44,250 --> 00:00:49,500
But in this case, we're using that mechanism as a way to say, don't slide up and down the ladder.

15
00:00:49,500 --> 00:00:54,960
So this will be our challenge to stop the player sliding, set the starting gravity on the player rigid

16
00:00:54,960 --> 00:00:57,480
body to the rigid bodies current gravity.

17
00:00:57,480 --> 00:01:03,390
So you'll need a way of finding out when we start our game what is the current gravity for the rigid

18
00:01:03,390 --> 00:01:03,990
body?

19
00:01:03,990 --> 00:01:10,650
And then to set a variable, which is to say this is the gravity when we first start our game and then

20
00:01:10,650 --> 00:01:14,400
when the player is on the ladder, set the gravity to zero.

21
00:01:14,400 --> 00:01:19,650
And obviously when the player is not on the ladder, then set the gravity back to what the starting

22
00:01:19,650 --> 00:01:20,930
gravity is supposed to be.

23
00:01:20,940 --> 00:01:21,510
So there you go.

24
00:01:21,540 --> 00:01:23,610
There's a bit of a different sort of challenge.

25
00:01:23,610 --> 00:01:24,360
Pause the video.

26
00:01:24,360 --> 00:01:24,930
Take that on.

27
00:01:24,930 --> 00:01:26,190
See back here when you're done.

28
00:01:28,500 --> 00:01:31,500
We'll be here in our player movement script up at the very top.

29
00:01:31,500 --> 00:01:38,100
I'm going to create myself another cached variable, which will be of type float, and that float will

30
00:01:38,100 --> 00:01:42,590
call gravity scale at start.

31
00:01:42,600 --> 00:01:47,520
Nice big name should make sense when we look back at this in the future and within my start method,

32
00:01:47,520 --> 00:01:49,230
I need to define that as something.

33
00:01:49,230 --> 00:01:56,070
So gravity scale start is going to be equal to my rigid body because this is where we find out gravity.

34
00:01:56,070 --> 00:01:58,350
Just a reminder, if you're like, how do you even know to do that?

35
00:01:58,350 --> 00:02:02,460
So if we go back to Rigid Body is the thing that controls gravity for the player.

36
00:02:02,460 --> 00:02:06,000
For this particular game object, you can see the gravity scale is eight.

37
00:02:06,000 --> 00:02:09,780
So it's rigid body 2d dot something to do with gravity.

38
00:02:09,780 --> 00:02:14,910
So if we jump in here, my rigid body, which is the way that we've cached our reference to the rigid

39
00:02:14,910 --> 00:02:23,130
body 2d so my rigid body dot gravity scale semicolon and that now case equals eight.

40
00:02:23,130 --> 00:02:27,030
But if we go and change our gravity scale in here, then it will be updated nicely.

41
00:02:27,030 --> 00:02:32,850
If we're not currently touching the ladder that we want the gravity to be the same as the starting gravity.

42
00:02:32,850 --> 00:02:34,890
So I'm going to expand this out now.

43
00:02:34,920 --> 00:02:38,700
My nice little neatly compartmentalized return.

44
00:02:38,700 --> 00:02:44,700
We'll put this on its own new line, new area return will stay at the bottom because if you're not touching

45
00:02:44,700 --> 00:02:47,010
it, we still want to return and not allow the player to climb.

46
00:02:47,010 --> 00:02:50,490
But I'll add something in here which will be my rigid body.

47
00:02:50,850 --> 00:02:56,490
Gravity scale is going to equal gravity scale at start.

48
00:02:56,820 --> 00:02:58,980
So I just want to make sure we've got the right gravity.

49
00:02:58,980 --> 00:03:02,600
So if you're not touching the ladder, then use the gravity scale that we had at the start.

50
00:03:02,610 --> 00:03:06,630
However, if you are currently climbing, then we'll pop down here.

51
00:03:06,930 --> 00:03:14,040
My rigid body dot gravity scale equals what equals zero.

52
00:03:14,040 --> 00:03:16,530
Don't give the player any gravity and it's a flow.

53
00:03:16,530 --> 00:03:19,290
We'll put f there just to be complete and thorough.

54
00:03:19,290 --> 00:03:22,770
We'll save that and see if we've got our desired effect.

55
00:03:22,800 --> 00:03:26,460
Gravity should be normal unless we're touching the ladder, in which case it should be zero.

56
00:03:26,490 --> 00:03:28,590
Click on play jumping works.

57
00:03:28,590 --> 00:03:30,060
Okay, climb the ladder works.

58
00:03:30,060 --> 00:03:30,390
Okay.

59
00:03:30,390 --> 00:03:31,320
And we're stopped here.

60
00:03:31,320 --> 00:03:35,820
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

61
00:03:35,820 --> 00:03:37,860
off the ladder, it goes back up to eight.

62
00:03:37,860 --> 00:03:38,370
Perfect.

63
00:03:38,370 --> 00:03:39,030
So there we go.

64
00:03:39,030 --> 00:03:41,760
We've got the right behavior that we wanted when we're on our ladder.

65
00:03:41,760 --> 00:03:44,370
We can't we're not slowly drifting down.

66
00:03:44,370 --> 00:03:46,140
We don't have our animation set up just yet.

67
00:03:46,140 --> 00:03:48,900
So I think it might be time to do that in the next lecture.

68
00:03:48,930 --> 00:03:49,410
Okay.

69
00:03:49,440 --> 00:03:50,850
See you in the next lecture.

