• 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!

Enemy knockback [SOLVED]

J

Jorex

Guest
im trying to make it so when the enemy makes contact with the player, the player bounces back away from the enemy. I managed to code knockback but it only works one way.
this is my code:
if (place_meeting(x,y,obj_enemySkeleton) && recenthurt = false)
{
flash = 3;
vspd = -4;
hspd = sign(obj_player.x-obj_enemySkeleton.x) * 4;
recenthurt = true;
audio_play_sound(snd_hurt,10,0);
alarm[1] = 2 * room_speed;
with (obj_stats)
{
hp--;
}
} else {
vspd += grav;
}
 
T

tehguy

Guest
Things to change
  1. check if the player is either on the left or right side of the enemy once contact is made
  2. adjust vspd as needed (-4 for left, 4 for right, for instance)
 

Bingdom

Googledom
The problem is that you're referencing a resource rather than an instance.

You need to get the id of the collided enemy (using instance_place), then do that sign() function, referring to the id.
 
J

Jorex

Guest
The problem is that you're referencing a resource rather than an instance.

You need to get the id of the collided enemy (using instance_place), then do that sign() function, referring to the id.
Thanks, that did the trick!
 
Top