Platformer issue - Character slides down ladder even when gravity is disabled

Hi there,

I’m working on a simple platformer in Phaser 3 and trying to implement ladders. Codepen is here:

Whilst on the ladder if the user is not pressing any controls then I want the character to just stay still. I’ve tried several things to get this working, 1. disabling gravity (player.body.setAllowGravity(false)) whilst on the ladder and then re-enabling it once they are off and 2. Giving the player a negative gravity to match the games gravity (in this case -300). It always results in the same thing though - the character moves very slowly downward as if still affected by gravity.

The way I’ve got the character to be still when on a ladder is to remove any y velocity when they overlap with ladder and then applying a slight negative velocity when on the ladder with no controls pressed:

player.setVelocityY(-5);

It feels like it should be 0 not -5? Does anyone have any idea why -5 is the magic number to make the character still? Whether or not the character is on a ladder is set by an overlap function in create:

this.physics.add.overlap(player, ladders, isOnLadder, null, this);

function isOnLadder() {
  onLadder = true;
}

And then at the end of my update game loop I am resetting the flag:

onLadder = false;

The offending line on the codepen is 448. If you change that to 0 instead of -5 you’ll see the character slips down the ladder when standing still on it (climb up half way then press nothing).

Here is a fork of the codepen where I am actually turning gravity on and off for the player as they go on and off the ladder and using setVelocity(0) instead of -5. You’ll see that the character slides down the ladder slowly:

Is the onLadder flag getting set in the wrong place, so for a split second the flag is false and the player then slowly slides down a tiny bit at a time? Does anyone have any ideas why it’s not working as expected? The random -5 as a solution doesn’t sit well with me! Any help or insight is greatly appreciated.

Thanks,

Tom