Detect player landing

FranFox

Member
Hi everyone! I'm trying to detect the moment when player lands on a platformer game but I don't understand why my code isn't working.

GML:
// We've just landed?
if place_meeting(x, y + 1, o_solid) and !place_meeting(x, yprevious + 1, o_solid)
{
    show_debug_message("Landed");
}
This should work, right? If I remove the ! on the second check is entering in the validation :S

Any suggestions? Thanks!
 

Slyddar

Member
x/yprevious will only work in the draw event. If you want to use it in the step event you can make your own xp/yp variables, and just set them to equal x/y at begin step.
 

MaxLos

Member
If your collision system is set up similarly, you can also do something like this:
Code:
//Vertical Collision
if (place_meeting(x,y+vsp,par_solid))
{
    while (!place_meeting(x,y+sign(vsp),par_solid))  y += sign(vsp);
    if (vsp > 0) //If we just landed
    {
         //Do something
    }
}
Just make sure to only apply gravity when your character is in the air
 

Joe Ellis

Member
I use a variable called air_time, which increases by 1 every step if there is no collision with ground.
I also use a ground boolean which is set to false before collision checks and true when it collides with ground.
Air time is also useful for handling things like jumping a fall damage

GML:
ground = false
collision_step()

if ground
{
if air_time > 5
{
//play landing sound, set landing animation and other things to do with landing
sprite_index = spr_landing
image_index = 0
audio_play_sound(snd_landing, 1, false)
if air_time > 60
{
//apply fall damage:
hp -= (air_time - 60) * fall_damage_ratio
}
}
air_time = 0
}
else
{
++air_time
vsp += grav
}

//Jumping
if keyboard_check(vk_jump)
&& air_time < 2
{
vsp = -jump_speed
sprite_index = spr_jump
image_index = 0
}
 
Top