Force push Enemy back from player collision

hello again, my friends.

I’m making a topdown and if my player collides with the enemy sprite… how do i make the player bounce back xx distance? (i use the word bounce lightly as not to confuse with bounce behaviour)

Add to Player an instant force to enemy.X() - 32 p/s on X axis and enemy.X() - 32 p/s on Y axis

This doesn’t work BTW… am i on the right track?

Thanks,
Rob

This video https://www.youtube.com/watch?v=ZjxWwf7RslU&ab_channel=GDevelop has a great knockback system, if you want to check it out.

I think it was made for enemy knockback, but you should easily be able to use the same concept for player knockback.

Thanks @weadsy1,

That didn’t quite work as I’d hoped.I guess I just need the enemy sprite to force back on itself by distance. I have played with forces but haven’t quite got there yet.

Does anyone know how to do thins please?
I’ve removed BOUNCE as this doesn’t work.

Hi again,
This is tough to explain without screenshots that I can reference, but essentially you need to give the player the same force as the enemy and rotate the angle by 180 degrees.

If not, you can also compare the position of the player and the enemy but that’s probably less flexible.

If you give screenshot of how you currently move the enemy, it should be easier to help you.

Hi @reina,

I’ve tried position/angle and a few other things :frowning:

I get what you mean in theory… the enemy should step back -64pixels if the sprite is 32pixels – right?

I feel I’m getting closer.

found this example from a Gdevelop tutorial: Conviction of gun dude desktop

But my code isn’t quite there.

Admittedly, I have no idea where the knockBackAngle comes from… it’s not in the Enemy object variables or anywhere else.

Eureka!
sorted it.

Please close this as RESOLVED.

1 Like