Enemy hit pushback/recoil

Hey there, im looking for help trying to create a recoil or pushback from enemy hits or encounters, where; if the player gets damage they are pushed back. I feel like I once saw a guideline for this but now that im looking for it I cant find it. Thanks!

What have you tried so far? Can you share any code or relevant events?

  • To move the player you want to use goto
  • To work out the new coordinates to move the player to you need to start with their current coordinates event.px and event.py
  • You'll also probably need the coordinates of the enemy. If an event is in the enemy tile script you can use event.x and event.y to get the coordinates of the enemy tile

Once you have the simplest version of knockback working then you can start improving it to deal with situations like the player being knocked back into a solid tile.

Hope that helps!

4 Likes

Thank you for the reply, been swamped a bit so I haven't been able to give the a shot, but when I get a chance I will let you know how it goes.

hmm well, I'm not sure what you're doing, but let's say you have some enemy located at enemy_x and enemy_y then, you could do something like this:

on knockBackPlayer do
  x = event.px
  y = event.py
  if enemy_x>x then
    x--
  end
  if enemy_x<x then
    x++
  end
  if enemy_y>y then
    y--
  end
  if enemy_y<y then
    y++
  end
  if x>=0 then
    if x<25 then
      if y>=0 then
        if y<15 then
          is_wall = solid x,y
          if is_wall==0 then
            goto x,y
          else
            // you could put in some special code for being pushed into solid things here
          end
        end
      end
    end
  end
end

this will only work if only adjacent enemies can push back (it isn't looking for diagonal pushes etc)
so it checks which side the enemy is on and changes the player x/y accordingly.
it also checks if they would be pushed into a solid object or into a wall before it actually does the forced movement (the goto statement)

1 Like