# How do I create a knockback-movement?

I am a web developer new to game development, and with limited math knowledge. I am writing a character movement controller in Unity. The game has an isometric Diablo-like camera. The character moves on mouse click within a limited area.

The character moves like this now:

I want it to move like this:

I don't need you to write the code for me: I just want to understand what the math for this looks like. A Unity- and C#-specific explanation would be great, but not necessary.

First code, code the normal movement of the character to progress a constant distance toward the target position every frame (ie. inside the Update() function).

Then put that movement code inside an if statement for if the character is being knocked back. Only move toward the target if not being knocked back.

If being knocked back, move toward the knockback target instead. Set the knockback target and the "is being knocked back" variable when the player is hit, and clear them when the knockback target is reached.

For the actual decelerating movement toward the knockback target, there are a number of ways to do something like that. One simple way is by decrementing the movement distance every frame; instead of moving a constant distance every frame, the "distance to move" variable gets smaller every frame.

Another way would be using what are called tweens, setting an "ease out" value for the movement. Tweens are slightly more complicated to setup, but once setup will make it very easy to do short targeted movements like this all over your game.

• wow...i have everything set up i just need to implement this thing. It looks great..thank a bunch mate :) You saved me probably days if not weeks :) Commented Jul 14, 2014 at 11:07

The proper way to make this 'knockback' movement is by applying an impulse on the knocked object. The impulse will be in the direction of the velocity of the hitting entity (the projectile), scaled to some number that fits your game (that should probably take into account the masses of the two objects).

To make the entity slow down gradually after the hit and eventually stop, you need to constantly (every frame) apply on it an 'air friction' force (actually more of an impulse applied every frame since it changes), which is a force opposite to the entity's current velocity, scaled to some number. Do this for all entities every frame. This will make them always slow down gradually after being hit. (This is also (generally) what makes a car in the real world not move forever after being hit by another car, but gradually slow down and stop moving). This will allow you to apply an impulse on an entity in a collision and then not worry about it anymore, things will take care of themselves.

To understand this you need to have a good basic understanding of vector math and basic physics. If you're not familiar with these topics I suggest you learn them. Once you understand them a lot of physics-stuff becomes simpler.