• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

knockback

J

joshua huxley

Guest
I need help, does anyone know a good knockback script for GM2?
 
N

noino

Guest
This is pretty vague. We need to know what you're already working with. Is it a platformer? Top down? What movement basis are you using?
(Basically show code xd)
 
J

joshua huxley

Guest
This is pretty vague. We need to know what you're already working with. Is it a platformer? Top down? What movement basis are you using?
(Basically show code xd)
a top down action rpg

and this is the code:

dir=point_direction(0,0,xaxis,yaxis);
if(xaxis=0 && yaxis=0)
{
len=0;
}else{
len=wsp;
}
hsp=lengthdir_x(len,dir);
vsp=lengthdir_y(len,dir);
 
N

noino

Guest
Not entirely sure what you mean by 'knockback' either, but at any point with the changes I made to your code below if the variable 'len' is set to a negative number it will kick backwards.
We can set the object into knockback by setting the 'len' variable to something negative.
For example:
Code:
kb = 5;
if(place_meeting(x,y,oObjectThatWillKnockTheObjectBack)){
    oObjectThatIsGettingKnockedBack.len = -kb;
}
But to not have it immediately reset the speed while negative we need to make it incrementally remove negative speed. You can adjust the '0.2' to be whatever you want depending on how quickly you want the player to recover from it.

Code:
dir=point_direction(0,0,xaxis,yaxis);
if(xaxis=0 && yaxis=0){
    len=0;
}else{
    if(len < 0){
        len += 0.2;
    }else{
        len=wsp;
    }
}
hsp=lengthdir_x(len,dir);
vsp=lengthdir_y(len,dir);
 
J

joshua huxley

Guest
Not entirely sure what you mean by 'knockback' either, but at any point with the changes I made to your code below if the variable 'len' is set to a negative number it will kick backwards.
We can set the object into knockback by setting the 'len' variable to something negative.
For example:
Code:
kb = 5;
if(place_meeting(x,y,oObjectThatWillKnockTheObjectBack)){
    oObjectThatIsGettingKnockedBack.len = -kb;
}
But to not have it immediately reset the speed while negative we need to make it incrementally remove negative speed. You can adjust the '0.2' to be whatever you want depending on how quickly you want the player to recover from it.

Code:
dir=point_direction(0,0,xaxis,yaxis);
if(xaxis=0 && yaxis=0){
    len=0;
}else{
    if(len < 0){
        len += 0.2;
    }else{
        len=wsp;
    }
}
hsp=lengthdir_x(len,dir);
vsp=lengthdir_y(len,dir);
I tried to use your code, but all it did was invert my player's movement
 
N

noino

Guest
did you add this part?:
Code:
if(len < 0){
       len += 0.2;
   }else{
       len=wsp;
   }
With this the movement will be reset after a short deceleration process. Or in other terms, knockback.
 
J

joshua huxley

Guest
did you add this part?:
Code:
if(len < 0){
       len += 0.2;
   }else{
       len=wsp;
   }
With this the movement will be reset after a short deceleration process. Or in other terms, knockback.
I found out the problem, it only works while the player is moving
 
Top