Spielerando

Knockback

Knockback pushes a character away from whatever hit it. Point the push away from the attacker, set the speed instead of adding to it, take control away until the push has faded, then give a short moment of protection. This keeps hits readable and stops one enemy from trapping the player.

Also called: Pushback, Hit push, Recoil.

A round creature with a lantern on its tail is flung through the air to the right after a swing from a tall scarecrow brute, a ring of pale shards bursting where the club hit.
Quillmarrow Deep, an invented side-on brawling adventure. Invented game, image generated with AI.

Paste this to your AI

Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.

Add "Knockback" to my game.

What it should do: A hit that moves the target tells the player what happened, makes space after a fight and gives weight to strong enemies. Without the stun and the protection it is only a shove, and a fair game turns into one the player cannot escape.

Build it exactly as this state machine. First check that it fits my game, then build it like this:

```mermaid
stateDiagram-v2
  [*] --> free
  free: Free
  launched: Pushed away
  recovering: Protected
  free --> launched: is hit
  launched --> recovering: control comes back
  launched --> free: back, no protection
  recovering --> free: protection ends
```

Use these starting values:
- knockbackSpeed: 9 units/s
- resistance: 0
- stunTime: 0.25 s
- protectionTime: 0.5 s
- runSpeed: 5 units/s
- maxPushSpeed: 24 units/s

Watch out for these common mistakes:
- The push adds to the speed: Tell your AI: set the speed to the push, never add to it.
- Attacker and character on the same spot: Tell your AI: when both are on the same spot, push in a fixed direction, here straight up.
- Trapped by hits: Tell your AI: ignore hits during the stun and for a short time after it. Here protection covers both.
- A long frame: Tell your AI: keep a share of the push that shrinks to zero at the end of the stun, instead of subtracting.
- Huge hits: Tell your AI: cap the push speed, and move the character in small steps if the cap is high.
- Heavy enemies and the stun: Tell your AI: if a heavy enemy should ignore hits completely, do not send the hit at all.

Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.

Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP

What it does

A hit that moves the target tells the player what happened, makes space after a fight and gives weight to strong enemies. Without the stun and the protection it is only a shove, and a fair game turns into one the player cannot escape.

How it runs, as a diagram

Everything it can be in, and what moves it on. Your AI builds from exactly this.

is hitcontrol comes backback, no protectionprotection endsFreePushed awayProtected
How it runs, as a diagram
  • Free → Pushed away: is hit
  • Pushed away → Protected: control comes back
  • Pushed away → Free: back, no protection
  • Protected → Free: protection ends

Good starting values

ValueStart withWhat it changes
knockbackSpeed9 units/sHow fast an ordinary hit sends the character away. It must beat the running speed, or running into the hit cancels the push.
resistance0How much of the push the character shrugs off. Zero takes all of it, one takes none. A heavy enemy gets a high value.
stunTime0.25 sHow long the player cannot steer, and how long the push lasts. About a quarter of a second is long enough to see the hit and short enough not to feel like a punishment.
protectionTime0.5 sExtra protection after control returns. The player needs time to get away before the next hit can land. Zero ends protection together with the stun.
runSpeed5 units/sHow fast the player moves when steering. It is the number a push has to beat.
maxPushSpeed24 units/sThe fastest any push can be. A huge hit would otherwise throw the character through walls or out of the level.

What usually goes wrong

The push adds to the speed

A push that adds to the current speed does nothing to a character running away from the hit and doubles for one running into it. Hits feel random.

Fix: Tell your AI: set the speed to the push, never add to it.

Attacker and character on the same spot

The direction is worked out from the difference of the two positions. When it is zero the division gives a number that is not a number, and the character vanishes from the screen.

Fix: Tell your AI: when both are on the same spot, push in a fixed direction, here straight up.

Trapped by hits

If every hit restarts the stun, two enemies or one fast attack can keep the player in the air for ever, without any chance to act.

Fix: Tell your AI: ignore hits during the stun and for a short time after it. Here protection covers both.

A long frame

Slowing down by subtracting a fixed amount per frame can go past zero in a slow frame, and the push turns around and drags the character back towards the attacker.

Fix: Tell your AI: keep a share of the push that shrinks to zero at the end of the stun, instead of subtracting.

Huge hits

A strong enemy or a stack of bonuses gives a push so fast the character passes through a wall in one frame.

Fix: Tell your AI: cap the push speed, and move the character in small steps if the cap is high.

Heavy enemies and the stun

Setting the resistance to its top removes the push but not the stun. The character still stumbles in place.

Fix: Tell your AI: if a heavy enemy should ignore hits completely, do not send the hit at all.

How to check it works

Works well with

Clashes with Combo System: A chain of hits on the player restarts the stun every time, and the player never gets to act. Protection covers the whole stun here, so a chain can only land again after it ends. Lower the protection time or let later hits push less if you want longer chains.

Common questions

what do i tell my ai to add knockback

Paste this: Add knockback: when a character is hit, set its speed to a push pointing away from the attacker, take away steering until the push has faded to zero, and ignore new hits until a short time after that.

should knockback add to the current speed or set it

Set it. Adding makes the same hit feel different depending on which way the character was running, and a character running away from the hit would hardly be pushed at all.

how long should the player lose control after a hit

Start at a quarter of a second. Shorter than a tenth of a second is hard to notice, longer than half a second starts to feel like the game took the controller away.

why does my character fly off the screen when hit

The attacker and the character are probably on the same spot, so the direction is not a number, or a strong hit has no upper limit. Give the same-spot case a fixed direction and cap the push speed.

Where it comes from

We could not find a reliable origin for this one yet.

Where this mechanic comes from has not been researched yet.

Games are named only as references. Names and marks belong to their owners, and nothing here is endorsed by them.

What was checked

This is what separates the blueprint from a catalogue entry: the values on this page come from code that runs.

Your AI gets the code

Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it