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:
And one where I’ve ‘fixed’ it by setting:
player.setVelocityY(-5);
On line 449
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.
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.
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:
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?
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.
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.