knockback problem

A

azzzanadra

Guest
ok guys, i have made code for knockback, however when the player is knocked back he enters the wall, something wrong with my collision, can someone check?

//horizontal collision
if (place_meeting(x+hsp,y,obj_wall))
{
while(!place_meeting(x+sign(hsp),y,obj_wall))
{
x +=sign(hsp)
}
hsp = 0
}
x += hsp;

//knockback
if (place_meeting(x,y,obj_enemy))
{
if obj_enemy.dir = -1
{
hit = 1
direction_of_attack = 1
}
}
else
{
hit = 0
}

if (place_meeting(x,y,obj_enemy))
{
if obj_enemy.dir = 1
{
hit = 1
direction_of_attack = -1
}
}
else
{
hit = 0
}

//knockback
if (hit = 1)
{
speed = -7
hsp = 0
alarm [0] = 20
key_right = false
key_left = false
}
else
{
key_right = keyboard_check(vk_right)
key_left = -keyboard_check(vk_left)
key_jump = keyboard_check_pressed(vk_space)
hsp = move * movespeed
}
 
A

Aura

Guest
That is happening because you're using speed for the knockback and the collision detection depends on the custom variable hsp.

The most feasible way would be to use hsp for the knockback as well. Try altering the code a bit and report back if you can't get it to work.
 
A

ajan-ko

Guest
Are you using knockback in sidescroller, or topdown?
Knockback in game maker really pain to deal with.
 
A

azzzanadra

Guest
side scroller.
the problem is i tried using hsp but the results weren't good.
 
A

ajan-ko

Guest
The thing is about side scroller, you need to be pixel perfect.
If you're using real
for example
x=5.555
or
speed=3.5
hell, even gravity
gravity=0.7
it will break your game.

So, in order to make sure my character not stuck in wall and do weird stuff,
I put script on my obj_character.
this is my event collision on obj_parent_block (or you can say object wall)
My obj_block same like the player character height x width.
If your character 32x32, make the obj_block 32x32 too, or else it's not gonna work.
Code:
    oddVar = 1;
    pixel_space = 10;

    if (hspeed < 0) oddVar=0;
    x=round(x)
    //--------going down---------
    if (bbox_bottom>other.y and bbox_bottom<other.y+other.sprite_height*3/4 and onGround){
        y=y-(bbox_bottom-other.y);
        vspeed=0;
    }

    //--------going right ---------
    if (bbox_right>other.x and
        bbox_right<other.x+other.sprite_width/2 and
        (position_meeting(bbox_right,bbox_top+pixel_space,obj_block) or
        position_meeting(bbox_right,bbox_bottom-pixel_space,obj_block))){
        if (onGround) x=x-(bbox_right-other.x)-2;
        if (!onGround) x=x-(bbox_right-other.x)-1;
        hspeed=0;
    }
    else
    //--------going left--------- oddvar3
    if (bbox_left>(other.x+other.sprite_width/2) and
        bbox_left<(other.x+other.sprite_width) and
        (position_meeting(bbox_left,bbox_top+pixel_space,obj_block) or
        position_meeting(bbox_left,bbox_bottom-pixel_space,obj_block))
        ){
        if (onGround) x=x+(other.x+other.sprite_width-bbox_left)+2;
        if (!onGround) x=x+(other.x+other.sprite_width-bbox_left)-1;
        hspeed=0;
    }
    //else
    //--------going up---------
    if (bbox_top>(other.y+other.sprite_height/2) and
        bbox_top<(other.y+other.sprite_height) and
        position_meeting(x,bbox_top,obj_block)
        ){
        y=y+(other.y+other.sprite_height-bbox_top);
        if (vspeed<0) vspeed=0;
    }
then I use custom on ground check to my character on event step.
Code:
    // ON GROUND CHECK!!
    if (position_meeting(bbox_right-1,bbox_bottom+1,obj_parent_block) or
       position_meeting(bbox_left+1,bbox_bottom+1,obj_parent_block) or
       position_meeting(x,bbox_bottom+1,obj_parent_block) )
    {
        if (onGround==0 and !knockStun) {onEndJump=1}
        onGround=1;
        doubleJump=2;
        doubleDash=1;
        gravity=0;
    } else {
        gravity=1;
        onGround=0;
    }
Again, each person have different cases, so I don't think this code works tho, but worth to try.
 
Last edited:
Top