• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Having issues with detecting objects directly below a player

I'm a huge noob at this and have been messing around in GMS 2 and decided to try and make a little stomp attack type thing where the player has to jump in the air and hit a button after they are high enough from the ground. But, for the life of me I can't figure out how to make it only check one instance of an object directly below, it keeps counting objects that are out to the right and prevents a stomp from happening. I've tried various methods and so far the only one that I could get to even slightly work right is obj_wall.y-obj_player.y>=128. Once again, I am a noob at this and have very little experience with programming, so any help will go a long way. My last ditch effort would be to pretty much glue an object to the player that is invisible and 128 pixels below that checks for collision, but I'd rather know if there is a simpler way.
 

kburkhart84

Firehammer Games
The simplest way is an "emulated" object. There are functions in the manual for checking collisions, both with actual object sprites, sprites as if they are in a given position, and basic primitives. The one that might fit for this purpose would be the collision_line() function. You would make it check a line that runs just below the player's position, from the left to right side of it, depending on how far out you want the check to apply. You can put that in the player's step event as a piece of code, and then it will return the id of the block if there is a collision. This does assume that you want to only have it collide with a single one at a time. If you want it to collide with multiples in the same jump landing, you could use collision_line_list() instead.
 
The simplest way is an "emulated" object. There are functions in the manual for checking collisions, both with actual object sprites, sprites as if they are in a given position, and basic primitives. The one that might fit for this purpose would be the collision_line() function. You would make it check a line that runs just below the player's position, from the left to right side of it, depending on how far out you want the check to apply. You can put that in the player's step event as a piece of code, and then it will return the id of the block if there is a collision. This does assume that you want to only have it collide with a single one at a time. If you want it to collide with multiples in the same jump landing, you could use collision_line_list() instead.
Thanks, this helped tremendously! Didn't know that function existed, probably skimmed over it when I went through the manual.
 

TheouAegis

Member
Code:
var target=noone;
with (target object parent)
if abs(x-other.x)<(sprite_width+other.sprite_width)/2 && y > other.y {
    target = id;
    break;
}
if target != noone
    //Slam down
 
Top