Ask coding questions

← Back to all posts
Player sometimes phases through platform one object?
EthanBlesch (0)

I have an infinite runner game that I'm coding and it has working jump, gravity, and infinite terrain functions, however I have not made it different, it's the same layout over and over for now, until i move on to that, but sometimes when jumped on, the player can phase partly through the platform one object, any idea why?

Comments
hotnewtop
notGilbert (79)

Sometimes, the player goes through the platform, because gravity is at a constant rate.

For example, if the player is 4 units above the platform and you move 10 units down, the player will be 6 units into the platform.

To solve this, use a while loop and repeatedly move up until the player is not colliding with the platform.

example pseudocode:

function updateGravity() {
  // move down if not colliding with platform
  if (!collidingWithPlatform()) player.y -= 10;
  
  while (collidingWithPlatform()) {
    // fix position glitching
    player.y += 1;
  }
}
EthanBlesch (0)

@notGilbert the function runs every game tick and moves down by one

notGilbert (79)

@EthanBlesch
i think the problem is that you arent doing collision checking from the side of each platform, so the player can phase in through the sides

EthanBlesch (0)

@notGilbert no, it happens even if you jump directly on top of it