Storm1208
Member
Hey all! I'm working on a 2D platforming infinite runner and there's a feature in it where after jumping, the player must press the space bar with perfect timing before hitting the ground. If they mess up, it causes them to trip and slow down.
My issue is that I can't seem to figure out how to gauge the distance between the player and the ground to check if the button timing was correct. Here's what my code looks like so far:
This current implementation isn't working. I also previously tried using GMLs "collision_line" function like so:
If anyone has experience with ground detection like this I'd love some help!
My issue is that I can't seem to figure out how to gauge the distance between the player and the ground to check if the button timing was correct. Here's what my code looks like so far:
GML:
if(space_key && vsp > 0 && (obj_ground.y - y < 80 && obj_ground.y - y > 5)) //if the space key is pressed, the player is falling, and they are within 5-80 pixels from the ground
{
//good case
show_debug_message("Good Landing!");
var onepixel = sign(vsp);
while(!place_meeting(x, y + onepixel, obj_ground))
{
y = y + onepixel;
}
vsp = 0;
my_state = player_state.on_ground;
}
else
{
if(vsp > 0 && obj_ground.y - y < 5) //else, the player was too close to the ground (I should probably check for space here too but it may not be necessary)
{
//bad case
show_debug_message("Bad Landing!");
var onepixel = sign(vsp);
while(!place_meeting(x, y + onepixel, obj_ground))
{
y = y + onepixel;
}
vsp = 0;
my_state = player_state.on_ground;
}
}
collision_line(x, y, x, y + 400, obj_ground, false, true);
but it wasn't working either. The game will eventually have more terrain than just "obj_ground" (I'm planning on adding sloped ground and grinding rails too) so another flaw in this system is it only detects for one type of ground object (obj_ground) even though I'm going to add more later.If anyone has experience with ground detection like this I'd love some help!