# Platformer Ladders and gravity

**URL:** <https://phaser.discourse.group/t/platformer-ladders-and-gravity/9463>\
**Category:** Phaser 3\
**Created:** [May 3, 2021, 4:18pm UTC](https://phaser.discourse.group/t/platformer-ladders-and-gravity/9463 "2021-05-03T16:18:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![digitherium](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/digitherium/32/5108_2.png) [@digitherium](https://phaser.discourse.group/u/digitherium)\
**Post date:** [May 3, 2021, 4:18pm UTC](https://phaser.discourse.group/t/platformer-ladders-and-gravity/9463/1 "2021-05-03T16:18:09Z")

</div>

Hi there,

I’ve got a weird issue with gravity in a platformer I am working on. When the player is on a ladder and not pressing up or down, I want them to just stay in place. I’ve tried disabling gravity when a player is on a ladder, and I’ve also tried giving the player the negative amount of gravity to counteract the global gravity. In both cases the player slowly slides down the ladder. It’s as if there is still a tiny bit of gravity left?

Here is a codepen of the issue:

https://codepen.io/digitherium/embed/preview/mdRYpod?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=mdRYpod

And one where I’ve ‘fixed’ it by setting:

`player.setVelocityY(-5);`

On line 449

https://codepen.io/digitherium/embed/preview/OJWGGZM?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=OJWGGZM

setting that value to -5 instead of 0 stops the player sliding down. I just can not for the life of me work out why that number needs to be -5? Any help or insight on this is greatly appreciated.

Cheers,

Tom

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [May 3, 2021, 10:19pm UTC](https://phaser.discourse.group/t/platformer-ladders-and-gravity/9463/2 "2021-05-03T22:19:07Z")

</div>

```
if (!onLadder) player.body.setAllowGravity(true);
onLadder = false;
```

---

<div class="post-metadata">

**Author:** ![digitherium](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/digitherium/32/5108_2.png) [@digitherium](https://phaser.discourse.group/u/digitherium)\
**Post date:** [May 4, 2021, 9:33pm UTC](https://phaser.discourse.group/t/platformer-ladders-and-gravity/9463/3 "2021-05-04T21:33:51Z")

</div>

Thanks, this works! I’m still not sure why it works though?

I was always turning the gravity back on at the end of the loop and then expecting that at the beginning of the loop if the player is on a ladder it’s shut off again and that’s effectively straight away. I guess what was happening was it was switched off, and put back on the next iteration of the loop, and in that split second the player is affected by gravity and starts falling? I wouldn’t expect that to happen, but my understanding of when the update loop is fired must be wrong.

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [May 5, 2021, 4:31am UTC](https://phaser.discourse.group/t/platformer-ladders-and-gravity/9463/4 "2021-05-05T04:31:34Z")

</div>

Physics is handled before scene.update. So setting gravity off and on in the same loop is not doing anything 🙂

---

<div class="post-metadata">

**Author:** ![digitherium](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/digitherium/32/5108_2.png) [@digitherium](https://phaser.discourse.group/u/digitherium)\
**Post date:** [May 5, 2021, 8:48am UTC](https://phaser.discourse.group/t/platformer-ladders-and-gravity/9463/5 "2021-05-05T08:48:37Z")

</div>

Sorry, checked this again and your solution doesn’t work. Character still slides down the ladder very slowly as if gravity is still taking effect:

https://codepen.io/digitherium/embed/preview/mdRYpod?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=mdRYpod

Resetting stuff at the end of the loop (i.e. turning the gravity back on) and then relying on flags to change that setting if they are set in the same loop is always how I thought you were supposed to handle things? Just because of the nature of the loop itself being called all the time, that will sort it self out. It’s just even with the gravity off the body stills moves down! Pulling my hair out with this one.

Should a body with 0 velocity with no gravity not just stay in place?

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [May 5, 2021, 9:27am UTC](https://phaser.discourse.group/t/platformer-ladders-and-gravity/9463/6 "2021-05-05T09:27:46Z")

</div>

You have discovered a bug 🙂

`this.physics.add.overlap(player, ladders, isOnLadder, null, this);`  
doesn’t trigger every frame.

@samme?

---

<div class="post-metadata">

**Author:** ![digitherium](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/digitherium/32/5108_2.png) [@digitherium](https://phaser.discourse.group/u/digitherium)\
**Post date:** [May 5, 2021, 1:21pm UTC](https://phaser.discourse.group/t/platformer-ladders-and-gravity/9463/7 "2021-05-05T13:21:08Z")

</div>

exciting stuff!

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [May 5, 2021, 4:55pm UTC](https://phaser.discourse.group/t/platformer-ladders-and-gravity/9463/8 "2021-05-05T16:55:51Z")

</div>

The fixed-step 60fps physics loop and the variable-step 60fps game loop are not synchronized. Usually they step together but rarely the game loop can step ahead for one frame.

Technically the correct way to handle this is to use the physics WORLD\_STEP event instead of scene `update()`. But you could also avoid it by raising the physics fps, perhaps to 120.

https://codepen.io/samme/embed/preview/ExZqBar?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=ExZqBar

---

<div class="post-metadata">

**Author:** ![digitherium](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/digitherium/32/5108_2.png) [@digitherium](https://phaser.discourse.group/u/digitherium)\
**Post date:** [May 5, 2021, 7:19pm UTC](https://phaser.discourse.group/t/platformer-ladders-and-gravity/9463/9 "2021-05-05T19:19:54Z")

</div>

Wow, next level stuff! Thanks so much for your help you two. I’ve noticed that even setting the FPS of the arcade physics to 60 has a big impact. The slide down the ladder still happens, but it’s barely noticable. Setting it to 120 does the trick fully.
