Need help with knockback

im making a topdown survival game where you can attack enemies but I need to know the best way to make knockback for my game My Project1-31-2023_12-29-22.wick (you automatically attack, there is no attack button).

like minecraft knockback?

yes, I need the enemies to be knocked back when hit, heres an updated file My Project1-31-2023_16-25-42.wick now the code fore the timed monster spawning is in the top left corner

im not good at javascript (like at all) but all i can think of is maybe code it to have like:
if this animation with this power lvl (which has knock back) plays then have enemy move back 10 meters (this is the only way i know to break it down

that’s basic trigonometry
you know how much your enemy will be pushed back
you know your position and the position of your enemy so you can calculate the angle of the line between the two
then find the missing side knowing one side and the angle
Google for “trigonometry find the missing sides” for help
Screenshot 2023-02-01 at 17-10-12 Wick Editor 1.19.3

remember that there’s a difference between degrees and radians. wick tends to use degrees but the trigonometric functions use radians.

and actually, by the looks of the project, you don’t even need trig. you already have it so that if an enemy touches the attack, it will lose health. so just also make it so that it will get pushed back based on what direction the player is pointing. so if the player is facing right, add ABC pixels to the enemy’s x position, and if it’s facing left, subtract ABC pixels.

I added knockback survivl2-1-2023_16-06-53.wick, what I did was make a variable for the speed and made the variable become negative when its hit and slowly goes back to positive.

This is the method I would’ve used, but in a different way.

If you were to find the difference between the x and y values of the player and the enemy, then add these to the x and y of the enemy, then they’d be moving away from the player.
There’s no need to find the angle in this case.
To increase the “knockback” effect, you can also multiply the difference by a number, or divide to decrease it.

enemy.x-=(enemy.x-player.x)
enemy.y-=(enemy.y-player.y)

I had to stop the clones from losing health to see it since they were dying too quickly. Your method seems to work great!

with your method the distance the enemy is pushed back does depends on player/enemy distance not hit strength
that could work in some case but you risk to have excessive pushback for long range hits
I imagined he wanted to have control on pushback strength (green line)

1 Like