Horizontal Platforming knockback for shields

Zapzel-24

Member
Lately I have been having issues with implementing a knockback code for a particular type enemy that carries a shield and can only be attacked from behind. When performing an attack on the enemy's shield the player is suppose to be slightly pushed back from the strike. However, when implementing the code there is zero reaction from the player object not even the slightest nudge. I have tried to implement a Place_meeting on the sheild's hitbox as well as a sign variable on the hsp movement but I cannot figure what i going on.

**It should be noted that the player object is functioning on a Finite State Machine and the knockback code is currently in the attack script where the player strikes a object (in this case a shield) it should slightly push back the player.

Code:
///scr_Player_Attack

//Input Script
scr_Player_Input();

vsp = vsp + grv

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

//Vertical Collision
if place_meeting(x,y+vsp,obj_wall)
{
    while (!place_meeting(x,y+sign(vsp),obj_wall))
    {
        y = y+sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;

//Attack Animation (Grounded)
if state = states.attack && image_xscale = 1 && vsp = 0
{
    instance_create(x+18,y,obj_hitbox);
    sprite_index = spr_Durga_attack;
    image_speed = 0.5;
    hsp = 0;
    //Collision knockback
    with (instance_create(x,y,obj_hitbox)) 
    {
        if instance_place(x,y,obj_shield)
        {
            var shield = (instance_place(x,y,obj_shield)) //<--- Having issue with this chunk of code.
            hsp = sign(x-shield.x)*1.5;
        }
    }
}
else
{
    if state =states.attack && image_xscale = -1 && vsp = 0
    {
        instance_create(x-18,y,obj_hitbox);
        sprite_index = spr_Durga_attack;
        image_speed = 0.5;
        hsp = 0;
        //Collision knockback
        with (instance_create(x,y,obj_hitbox))
        {
            if instance_place(x,y,obj_shield)
            {
                var shield = (instance_place(x,y,obj_shield)) // Same as the forst but for the opposite direction.
                hsp = sign(x-shield.x)*1.5;
            }
        }
    }
}

//Attack Animation(Ariel)
if state = states.attack
{
    if image_xscale = 1
{
    instance_create(x+18,y,obj_hitbox);
    sprite_index = spr_Durga_attack;
    image_speed = 0.5;
}
else
{
    if image_xscale = -1
    {
        instance_create(x-18,y,obj_hitbox);
        sprite_index = spr_Durga_attack;
        image_speed = 0.5;
    }
}   
}

//Return to Walking Script
if (image_index > image_number -1)
{
    state = states.walking
    image_speed = 0.1;
}
 

Bentley

Member
Just a heads up, "x - shield.x" doesn't check the player's x position, it checks the hitbox's x position. Ignore this post if that's on purpose.
 

NightFrost

Member
Seems like that script can spawn up to three obj_hitboxes in a single pass. Anyhow, I don't see you setting a knockback state or anything that would flag a continuous forced movement state. You are trying to set x-position back 1.5 pixels once if the second hitbox you spawn collides with obj_shield, but since even that is inside the with-block, it is setting a hsp variable for the hitbox.
 
Top