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.

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.
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
| Value | Start with | What it changes |
|---|---|---|
| knockbackSpeed | 9 units/s | How fast an ordinary hit sends the character away. It must beat the running speed, or running into the hit cancels the push. |
| resistance | 0 | How much of the push the character shrugs off. Zero takes all of it, one takes none. A heavy enemy gets a high value. |
| stunTime | 0.25 s | How 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. |
| protectionTime | 0.5 s | Extra protection after control returns. The player needs time to get away before the next hit can land. Zero ends protection together with the stun. |
| runSpeed | 5 units/s | How fast the player moves when steering. It is the number a push has to beat. |
| maxPushSpeed | 24 units/s | The 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
- An attacker stands to the left of your character. The attacker hits: The character is pushed to the right at the knockback speed. From an attacker below and to the left, the push goes up and to the right..
- One character stands still and another runs towards the attacker. Both are hit in the same way: Both are pushed at exactly the same speed.
- Your character is hit twice, once with double power and once with half power. You compare the two pushes: The strong hit pushes four times as fast as the weak one.
- Your character has just been hit. The player holds the stick against the push: The movement is the same as when the player does nothing.
- Your character has been hit. The stun time has passed and the player steers: The character moves at the running speed again.
- Your character has just been hit. The stun runs down: The push gets slower in a straight line and is exactly zero when control returns.
- Your character has been hit and control is back, but protection is still running. Another hit arrives: Nothing changes: no push, no new stun.
- Your character was hit and a full second has passed. Another hit arrives: The character is pushed and stunned again.
- The attacker and your character are on exactly the same spot. The attacker hits: The character is pushed straight up and every number stays valid.
- Your character has half resistance, and another has full resistance. Both are hit: The first is pushed half as fast, the second is not pushed but still stunned.
- A hit with a hundred times the normal power. It lands: The push is exactly the maximum push speed.
- A hit with no power, or less than none. It lands: The character is not pushed, not stunned and not protected.
Works well with
- Combo System: A bigger push on the last hit of a chain gives a clear ending. Let the combo decide the power of each hit, then hand that power to the push.
- Stamina System: Our tests here do not cover the two together. Take stamina for a block before the push, and shrink the push by the block.
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.
What was checked
- 19 tests pass on the reference code.
- The state diagram was checked against the code, at the default values and at every listed low and high value.
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