How to make GO bounce after being attacked? "Knockback" (SOLVED)

So basically, like in most top-down adventure games and RPGs I want the player to bounce away from an attack from an enemy. The enemy should also bounce away from an attack after being hit. How would I accomplish this? I’ve done some research on google but it’s difficult to figure out how to do it. :sweat_smile: Basically the problem is I don’t know how exactly to calculate this? Like I’m assuming maybe something like the direction the enemy was facing, assuming the enemy was chasing the player, then the player should bounce in that direction when hit. But what if the object is stationary? How would I calculate then? Also I should clarify I’m not using physics simulation, I’m using kinematic and trigger collisions. :sweat:

zelda-the-legend-of-zelda

edit: Added gif to show what I mean
edit 2: Added a little more info and clarification about what I need help with
edit3: Further clarifaction

1 Like

You can use go.animate and for instance animate both “position.x” and “position.y” separately to achieve something like this.

Okay, that seems like it would work. But how would I calculate the direction vector to achieve the proper direction to bounce away? :sweat_smile: I’m pretty sure that’s a simple programming thing, not specific to defold, but I can’t find anything that is understandable (most of it is unity specific stuff)

If you get the position of the player and the enemy and subtract one from the other you get the difference. If you then normalize the difference you get a direction. Use this direction to calculate the pushback.

2 Likes

Oh, and here’s an example suitable for projectile knock-back: Knockback

4 Likes

Works perfectly, thanks. :grinning_face_with_smiling_eyes:

1 Like