SOLVED My Character's knockback is only half working and I don't know how to fix it.

I'm trying to program a knockback for when the caracther is hit by an enemy, but it's olny half working.

GML:
if (place_meeting(x+sign(move_speed), y, Obj_Slug)) {
        x-=move_speed*15;
}

if (place_meeting(x-sign(move_speed), y, Obj_Slug)) {
        x+=move_speed*15;
}

if (place_meeting(x, y-sign(move_speed), Obj_Slug)) {
        y-=move_speed*15;
}

if (place_meeting(x, y+sign(move_speed), Obj_Slug)) {
        y+=move_speed*15;
}
Only the horizontal knocback is working, but when I delete this part (first half of the code), the vertical one starts working.
Could someone help me? I'm still learning and this is my first project. Every content about knockbacks are from 2 years ago or older, so none of them helped me.
I'm sorry if this is an easy stuff and there are dumb mistakes, but as I said, I'm still learning.
 

curato

Member
is it possible that the x move is moving it out of range for the y move or it you either move x or y?
 
Shouldn't the signs be flipped when you apply the y movement, similar to how the x is set up (for example, you check x+ but then apply x-).

Also, if you wanted knockback that pushes the player away in an infinite number of directions instead of just 4, you could implement something like this (otherwise just ignore the code):

GML:
nearest_slug = instance_nearest(x, y, Obj_Slug);
if (place_meeting(x, y, nearest_slug)){
    knockback_direction = point_direction(nearest_slug.x, nearest_slug.y, x, y);
    x += lengthdir_x(move_speed*15, knockback_direction);
    y += lengthdir_y(move_speed*15, knockback_direction);
}
 
Shouldn't the signs be flipped when you apply the y movement, similar to how the x is set up (for example, you check x+ but then apply x-).

Also, if you wanted knockback that pushes the player away in an infinite number of directions instead of just 4, you could implement something like this (otherwise just ignore the code):

GML:
nearest_slug = instance_nearest(x, y, Obj_Slug);
if (place_meeting(x, y, nearest_slug)){
    knockback_direction = point_direction(nearest_slug.x, nearest_slug.y, x, y);
    x += lengthdir_x(move_speed*15, knockback_direction);
    y += lengthdir_y(move_speed*15, knockback_direction);
}
I tried to flip the signs, but it just didn't work. But your code WORKED! Thanks so much for your help!
 
Top