Knockback effect, stuck in wall after knocked back

✓ Resolved

Hello there everyone, I think I've put too much time into figuring this out by myself and I would like some input.

I am using GameMaker Professional Edition 1.4.1567 and I am using GML.

I have been using Shaun Spaulding's platformer tutorials and would really appreciate if someone could help me tie in a knockback mechanic where the player won't get stuck in the wall and won't have the player stutter when being pushed against a wall.

His code is quite ingenious and if there is a way to incorporate the variables he's already created, that would be really wonderful to know how to apply the knockback concept to it. This is his collision/movement code in the player object step event.

//Respond to left and right keys
move = key_left + key_right;
hsp = move * movespeed;

//Falling/gravity code
if (vsp < 10) vsp += grav;

//Jumping code
if (key_jump) vsp = -jumpspeed;

//Variable jump height (holding down shorter/longer) code
if (vsp < 0) && (!key_jump_held) vsp = max(vsp,-jumpspeed/4)

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

}

x += hsp_final;

var vsp_final = vsp

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

y += vsp_final;

Please bare with me as I just recently started using GML and trying to understand it. My code and thought process is very sloppy compared to many programmers and I hope this can be a learning process for me.

So here's my code, starting with the enemy object's step event, when they collide with the player, they set a knockback variable to 1 to check if the player is knocked back, and a timer that counts down so the player can't be knocked back continuously:

if (place_meeting(x-1,y,obj_player)) && (obj_player.knockback = 0) //Enemy on left, player on right
{
if (obj_player.y < y-16)
{
    with (obj_player) vsp = -jumpspeed;
    instance_destroy();
}   
else
{
obj_player.knockback = 1;
obj_player.knockback_time = 30;
obj_player.hp -= 10;
}
}

if (place_meeting(x+1,y,obj_player)) && (obj_player.knockback = 0) //Enemy on right, player on left
{
if (obj_player.y < y-16)
{
    with (obj_player) vsp = -jumpspeed;
    instance_destroy();
}   
else
{
obj_player.knockback = 1;
obj_player.knockback_time = 30;
obj_player.hp -= 10;
}
}

This is the code for the player object's step event when they are knocked back, they move upwards and backwards from the enemy and the knockback timer ticks downward to 0 when they can be knocked back again:

//In player object's step event
//Knockback code


if (knockback = 1) && ((abs((obj_wall.x)-(self.x))) > 16) && (!place_meeting(x+10,y-6,obj_wall))
{
    x += -5 * sign(image_xscale);
    y += -3;
}

if (knockback = 1) && ((abs((obj_wall.x)-(self.x))) > 16) && (!place_meeting(x-10,y-6,obj_wall))
{
    x += -4 * sign(image_xscale);
    y += -2;
}

if (knockback = 1) 
{
grav = 0.5
knockback_time -= 1;
}

if (knockback_time <= 0) 
{
knockback = 0;
grav = 0.2;
}

if place_meeting(x,y,obj_wall) && (!place_meeting(x,y,obj_enemy))
{
knockback = 0;
x = xprevious;
y = yprevious-1;
}


if place_meeting(x,y,obj_wall) && (place_meeting(x,y,obj_enemy))
{
x = xprevious;
y = yprevious-3;
}            

My question is how can I work with what I've got so far to cause the player object not to stutter and not to get stuck in walls. So far with the code, the player won't get stuck in walls anymore, but stutters if he is close to a wall when colliding with the enemy. I've set it up so that the knockback timer resets if the player object is against a wall but is not near an enemy so that the player doesn't continue to move into it. I've also set up code so that the player moves up and away if they are near a wall and an enemy.

I've spent quite a bit of time trying to work this out on my own and I would really like input from people who can wrap their head around this stuff. Thank you so much for any help!

Posted May 10, 2015, 4:39 AM UTC

59
ZeCatox•

Your last piece of code isn't quite clear to me, but what does look like a mistake in it is the way you're refering to obj_wall.x or obj_wall.y here and there.

That method works when there is only one instance of the object in the room, such as obj_player. But here, there are certainly many obj_wall's instances, and using obj_wall.x won't have the line executed for each of its instance : obj_wall.x will only refer to the x value of the first instance of obj_wall in the room.

You could make the test for each instance of obj wall, using a with statement. I can't delve into this method of doing right now (I'm not at my place), and I'm not sure you're going the right way anyway.

You could also approach the the problem in a different fashion : what if a knockback was like pressing a big "left/up" button on the keyboard ?
In other words : when knockback is applied, simply have vsp/hsp modified so that the movement of the player would make it go up/back very fast. Your step event is already taking care of the moving and collisions with walls, after all.

2
randomnewbOP•

Thank you so much for your reply! I am going to read over your response a couple times and test it again. I'll report back when I've tried it.

1
randomnewbOP•

Alright, I got it to work. I did try incorporating the original variables from Shaun Spaulding's tutorial, I just didn't work hard enough... I ended up using the collision/movement code from there in conjunction with the knockback variable and here is the result!

Enemy Object Step Event

if (place_meeting(x,y,obj_player)) && (obj_player.knockback = 0) //Enemy on left, player on right
{
    if (obj_player.y < y-16)
    {
        with (obj_player) vsp = -jumpspeed;
        instance_destroy();
    }   
    else
    {
    obj_player.knockback = 1;
    obj_player.knockback_time = 20;
    obj_player.hp -= 10;
    }
}

And here is the player object step event:

Player Object Step Event

//React to inputs
if (knockback = 0)
    {
    move = key_left + key_right;
    hsp = move * movespeed;
    }

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

//Added this piece after the horizontal collision code
if knockback = 1 
    {
    hsp_final = (image_xscale)*5;
    }

//Need a copy of the above with knockback = 1 and piece above MUST come before it or game crashes/freezes
if (place_meeting(x+hsp_final,y,obj_wall)) && (knockback = 1)
{
    while(!place_meeting(x+sign(hsp_final),y,obj_wall))
    {
        x += sign(hsp_final);
    }
    hsp_final = 0;
    hsp = 0; 
}

x += hsp_final;

var vsp_final = vsp

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

//This MUST come before the piece below
if knockback = 1 
    {
    jumpspeed = 3;
    jumps = 0;
    vsp = -jumpspeed;
    jumpspeed = jumpspeed_normal;
    }

//Same as horizontal, need a copy of the vertical collision code and knockback = 0 and 1 are separated... I can't use OR for some reason
if (place_meeting(x,y+vsp,obj_wall)) && (knockback = 1)
{
    while(!place_meeting(x,y+sign(vsp),obj_wall))
    {
        y += sign(vsp);
    }
    vsp_final = 0;
    vsp = 0;
}

y += vsp_final;

So there you have it. It works very, very well, and I can adjust the knockback height and distance and there is no stuttering and no wall clipping with the above code. I don't understand why I can't combine knockback = 0 || knockback = 1 so that I don't have to copy it twice though. :/

1
ZeCatox•

I have a hard time focusing right now. Here is what I could come up with.

In the player object step event, I'd say you have inconsistencies going on in the way you do things, and I suspect those are the reason for your freezes/crashes. And even if you found a workaround, I think it's still there.

Basically, what I see in pseudo language :

if knockback==0
   set 'hsp' value

if knockback==0 and x+hsp_final hits a wall // not using hsp ?
   while x+hsp_final hits a wall
       x+=sign(hsp_final) // if hsp_final==0, then we have an endless loop
   hsp_final = 0; // <- and here we are setting it to 0
   hsp=0; // non-important

if knockback==1
   set hs_final value // now we're using hsp_final ?

if knockback==1 and x+hsp_final hits a wall // apparently yes
   while x+hsp_final hits a wall
       x+=sign(hsp_final) // clearly not an endless loop here
   hsp_final = 0; // <- and here we are setting it to 0
   hsp=0; // non-important

At this point, I'd say hitting a point while not knocked back by an enemy should lead to a freeze most of the time.

Now the vertical part.

vsp_final = vsp; // note this was missing in the horizontal part

if knockback==0 and y+vsp hits a wall
    while y+vsp hits a wall
        y+=sign(vsp);
    vsp_final=0;
    vsp = 0;

if knockback==1
    jumpspeed=3 // why this
    jumps=0
    vsp = -jumpseed
    jumpspeed = jumpspeed_normal // if this ?

if knockback==1 and y+vsp hits a wall
    while y+vsp hits a wall
        y+=sign(vsp);
    vsp_final=0;
    vsp = 0;

y+=vsp_final;

This vertical part seems way better and quite straightforward. You should be able to add -jumpspeed to vsp if knockback and have the while code just once instead of twice. Something like :

vsp_final = vsp; 

if knockback==1
    vsp -= 3
    jumps=0
    jumpspeed = jumpspeed_normal 

if y+vsp hits a wall // NOTE : maybe you should also check that vps!=0 to avoid infinite loops
    while y+vsp hits a wall
        y+=sign(vsp);
    vsp_final=0;
    vsp = 0;

y+=vsp_final;

If that makes sense and works correctly, maybe you can translate it to horizontal part to make that one better too.

Sorry if that's not completely clear, lots of children noises around me >__<

1
randomnewbOP•

Ahhh, I'm so sorry, did you not see my reply? :/ I actually worked through it and solved it. I'm working on new features now... Thank you for taking the time to help me and write a nice response out. Maybe other people who see this thread can get help from us.

1
ZeCatox•

I don't see an other reply except the one I was answering to, where you wonder about not being able to combine knockback==0 or knockback==1.

But if you're good, then it's all good :)

1