Overlap does not always set variable each update

Hello everyone, i’m building a simple platformer and about to add climbing on ladders/vines etc.

The desired behavior for the character is to turn off gravity and y speed once it touches a ladder object.

I make use of the physics arcade overlap function:

        this.physics.add.overlap(this.player, this.ladders, function (player,ladder) {
            player.onLadder = true;

Then in the update i set it back to false to reset the onLadder var:

    update (time, delta)
       PlayerMovement(); //do movement on ladder or normal movement (if onladder is false turn on gravity)
       this.player.onLadder = false; //reset vaiable so the overlap can be checked next frame

This works for the most part, but while staying inside of a ladder object sometimes the onLadder variabel does not get set to true, and thus every other frame the character starts sliding (slowly) to the bottom.

I added a test var, and every overlap call i add to it “test++”.
In the update function i console log the value and set it to zero for the next frame.
every 2 frames it gets called 3 times… while the other 2 frames it does not get called and thus the character gets its gravity back for 2 frames…

This happens because the fixed-step physics update isn’t synchronized with the scene update.

One solution is to switch to the variable-step physics update. Another is to remove the collider and instead make an overlap test in update():

function update(time, delta) {
  this.player.onLadder = this.physics.overlap(this.player, this.ladders);

Also I think you need to use setGravity(0, -300).

1 Like

Thankyou Samme for your quick reply!

I fixed it today using you second suggestion.

1 Like