Object that bounces Player in 2 directions

Hello,

I want to create an object that bounces the player in two directions. The direction depends on where the player touches the object.
If he touches it by jumping on it he goes upwards. (like a bonus jump) and if he touches it from the bottom he gets kicked downwards.

My code looks like this so far:
GML:
//in collision with object
    if (place_meeting(x,y+1,o_ballon)) {
        variable_jump = false;
        vsp = -3.2;
        }
    else if (place_meeting(x,y-1,o_ballon)) {
        variable_jump = false;
        vsp = 0;
        vsp = 1.6;
        }
}

//in Step I use a very weird variable jump code. Maybe that causes some errors
//Jump
if (place_meeting(x,y+1,o_ground)) {
    variable_jump = false;
    on_ground = true;
    jump = 1;
    }
    
if (key_jump) && (jump > 0) {
    variable_jump = true;
    on_ground = false;
    vsp = -3.2;
    jump --;
    }

//variable Jump
if (variable_jump == true) {
    if (vsp < 0) && (!key_jump_held) {
        vsp = max(vsp, (-3.2/4));
        }
}
Somehow if the player touches the object from the side it is a bit random if he gets kicked up or down.
Mostly its up because the player comes in from the side and that mostly counts as hitting it from the top when it clearly looks like the bottom...
Anyone has a better idea how to handle this?
 

poorlocke

Member
Just add a final place meeting where both y +1 and y - 1 conditions are true and decide if you want a jump. That's not the optimal way but it doesn't interfere with the rest of your code much
 
Is there maybe a way to find out if the player is touchng it more on the top or more on the bottom by simply doing player - top and player - bottom and then see what is the bigger number?
 

poorlocke

Member
Is there maybe a way to find out if the player is touchng it more on the top or more on the bottom by simply doing player - top and player - bottom and then see what is the bigger number?
Sure. Provided that the player's y origin is in the middle of their collision mask I guess you can simply do
GML:
if place_meeting(x, y + vspeed, obj_baloon){
var touching_baloon = instance_nearest(obj_baloon);
var top_distance = abs(touching_baloon.bbox_top - y);
var bottom_distance = abs(touching_baloon.bbox_bottom - y);

if top_distance <= bottom_distance{
//Insert code for touching baloon's top
}
else{
//Insert code for touching baloon's bottom
}
}
I don't know how optimized this is though, just code that came on the top of my head
 
Top