1

trying to create a player damage indication when a collision with an enemy occurs.

I used this code within a collision event of the player object:

direction = point_direction(other.x,other.y,x,y);
hsp = lengthdir_x(6,direction);
vsp = lengthdir_y(4,direction)-2;
if (sign(hsp) !=0) image_xscale = sign(hsp); 

However, the player object is simply pushed upward vertically rather than backwards in a parabola. Any, ideas on how to implement a basic knockback system?

2 Answers2

0

If you want a parabola, you can add an upward force afterward, like so:

direction = point_direction(other.x, other.y, x , y);
speed = 6
motion_add(90, 3)

If you don't and you'd rather a more "repeatable" parabola that always look the same, maybe you should use another method, something like

if other.x>x {hdirection=1}else{hdirection=-1}
hspeed = hdirection*6
vspeed = -2

I believe this would work better for what you're trying to achieve, unless you want to implement knockback variable depending on the angle of collision.

Fox
  • 16
  • 1
0

So I would need to see all the rest of your player physics to be sure, but I can tell you right now that direction = point_direction(other.x,other.y,x,y); is probable not what you mean, and same goes for lengthdir(). The exact origins of the colliding objects at the moment of collision have a huge effect on what that direction is, which can cause a lot of screwiness. For example: if the line is perfectly horizontal (because other.y == y) then lengthdir_y() will always be equal to zero for any input value no matter how huge.

But more importantly direction is a built-in variable of GameMaker, so using it with a custom physics system can also cause some screwiness. Fox's answer might help if you are using built-ins, but based on the fact that you have an "hsp" and "vsp" instead of hspeed and vspeed, my guess is you want to avoid built-ins.

If you just want to get the horizontal direction of the collision, you should use sign(x - other.x). Then, instead of using lengthdir(), you can just use a constant. Here it is all together:

var dir = sign(x - other.x)
hsp = 6*dir; //I'm assuming that 6 is how much horizontal you wanted
vsp = -4;
if (sign(hsp) !=0) image_xscale = sign(hsp); //This line could just go in your step event

Hope that helps! Feel free to comment if you have more questions.