Skip to content

03 / Impact

Knockback

shove the target away from the blow

Knockback shoves the target away from whatever hit it, then lets friction stop the slide. The distance tells the player how strong the blow was and where it came from, and the gap it opens gives both sides room to reset. With it off, the target soaks the hit in place; with it on, it skids back and settles.

Use it for
  • Melee and projectile hits
  • Explosions, which push everything outward from the blast

Prompts to paste

Prompt line

On hit, set the target's velocity along the line from attacker to target, scaled by the attack's knockback force over the target's weight, lock out its own movement input for about 0.2 s, and let friction slow it rather than moving it a fixed distance.

Full prompt

Implement knockback in the hit handler. Work out the direction from the attacker to the target (on the ground plane, plus a slight upward part in 3D), and set the target's velocity to that direction times attack.knockback / target.weight. Put the target into a 0.2 s hitstun state that ignores its movement input while physics and collisions keep running, and let ground friction bring it to rest. Make knockback, weight and hitstun tunable.

Examples

Direction comes from the hit

Push along the line from the attacker, not the way the target is facing, so the slide always points away from the blow.

Weight tells a story

Dividing by weight sends small enemies flying and barely moves big ones, which tells the player what they're up against.

A little lift in 3D

A small upward part of the push keeps targets from catching on bumps in the floor.

Pitfalls

Teleporting instead of pushing

Moving the position directly skips collision checks, so a target can end up inside a wall. Set the velocity and let physics move it.

Forgetting hitstun

If the target's own movement code keeps running, it overwrites the push on the next frame and nothing visibly happens.

Too much for the arena

If every hit sends enemies out of reach, combos fall apart. Match the force to the attack's range.

Starting values

SettingValueNote
Push speed4–10 m/s in 3D, 150–400 px/s in 2D
Frictionbrings the slide to rest in 0.15–0.3 s
Hitstun0.15–0.3 s