I have frequently tried to make some sort of a platformer, but when I actually code it, I run into a bit of a snag. How would I be able to make platforms in the air stop objects if they hit the sides or bottom, but allow them to sit on top of them?
I have tried many different methods, and some work slightly, but have many bugs, or some don't even work at all. Like this one I tried made it so that if you hit the side of the platform at the right angle, it forced you up on top of it.
Answer
Without knowing much about how your code works it's hard to give an exact answer, but something like this should work:
if (collision(platform) && pos.y - height - vel.y < platform.y && vel.y > 0) {
// Player collides, was over the platform the last update and is falling
vel.y = 0; // Stop player
pos.y = platform.y - height; // Position player on platform
onGround = true;
}
If the registration point of the player is centered you would use height/2 instead of height, and you might have to take the platforms height into account as well if it's registration point isn't at the top.
No comments:
Post a Comment