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
 
I am back. After the code worked a bit better than mine, but not perfect I gave up on it for months. But after checking my code again i found out that the sprite Origin was set to Custom and not to middle centre. Now it works fine.
I am really stupid not checking that. But I'm happy it finally works. But my next problem would be making it a solid object. So you can only bounce if you touch it from top or bottom but if you touch it left or right you stop.
I already tried making it a parent or a child of my ground object (o_ground) but it didn't work. now I am again stuck. Any suggestions?
 
The code is in the player object. My ground object has no code.

GML:
if (place_meeting(x+hsp,y,o_ground)) {
    while (!place_meeting(x+sign(hsp),y,o_ground)) {
        x = x + sign(hsp);
        }
    hsp = 0;
    }

x = x + hsp;
But just adding the code again with the bounce object doesn't work. And if I make it a parent of the ground, the bounce won't work anymore. I want the object ob the sides to be solid, but bounce on the top and bottom.
 
Last edited:
Top