To make gameplay dynamic, a lot of little effects are necessary. One such effect is a recoil on hit: remember when you played a Zelda game? A monster hit by an attack would get pushed back, sometimes falling off into a hole, never to be seen again. Or it could be you being pushed off a platform by a tricky boss.
We’ll be working to implement this effect in Unity. A first word: we’ll be implementing it for scratch, but you might prefer using rigidbodies.
We’ll start by setting up for our demonstration scene. We’ll create a controller which triggers the OnHit method on a GameObject when we press Space. It will send 2 pieces of information: the direction and the force of the hit.
Now that this is set, we can focus on the OnHit behaviour itself on our monster / player.
For our effect, we want the unit to be pushed back, but we also want to add some decay to it: the pushing force should not be constant but should instead start strong and decay over time. This means we need to set a duration for the recoil effect, and a variable tracking the elapsed time for the recoil effect, as well as the direction of the force and its strength.
Considering all this, we can adjust the force currently pushing the unit by a factor of: 1f - elapsed_time / recoil_duration. If we then square this value, we’ll get a very nice decay: a strong push at first which becomes weaker and weaker over time.
We’ll also use a variable, being_hit, to signify that the character is currently being pushed. Once elapsed_time > recoil_duration, we can set being_hit to false and go back to the default behaviour.
As a bonus, we are also changing the color of the Sprite when it is hit, fading the tint from red to the base white along with the pushback Pretty neat!
Now let’s see how it looks:
Very nicely done, we now have a working recoil effect! You’ll just need to add a link your collision map, or check for pitfalls and you’ll have recreated an important mechanic for ARPGs, congratulations!