Player pushback when collide with enemy

I want a create a effect similar to Megaman in my platform when you hit an enemy. As in, the player gets pushed back a bit when he takes damage. My player is a rigid body, so I know I should use the AddForce, probably using an Impulse, however I’m unsure if I’m using this right as nothing is happening. Does anyone have an example of Addforce being used for something similar?
Here is my test function.

public void takeDamage()
//apply actual force
rigidbody.AddForce(-Test, ForceMode.Impulse);

Where -Test is the player’s current velocity mirrored.

You can use the velocity to do this : velocity.x = -1 or something like that (use a temp vector 3 to stock velocity).

You should not use the player velocity inverted to push it back, because nothing will happen when the player velocity is zero. There are some well defined strategies to apply the impact, depending on what is hitting the player:

  • If your player is a rigidbody, you don’t need to do anything when the enemy hits the player with another rigidbody (the projectile): physics will generate the appropriate reactions to both, the player and the projectile, and your only job is to choose suitable masses for each one - hitting a mass 1 player with a mass 1 bullet will throw it back too far, thus the bullet and the player must have a reasonable mass ratio (like player mass equal to 10 times the bullet mass, for instance).

  • Things are different when the enemy hits the player with a raycast shot: since there’s no actual projectile, you must simulate the impact in the shooting code, like this:

Enemy weapon script:

var impactForce: float = 20;

function RaycastShot(){ // function that actually does the shot
  var hit: RaycastHit;
  // shoot in the weapon forward direction:
  if (Physics.Raycast(transform.position, transform.forward, hit)){
    if (hit.rigidbody){ // if any rigidbody hit...
      // apply impactForce in the shot direction:
      hit.rigidbody.AddForce(transform.forward * impactForce);
  • If the enemy hits the player with a rocket, grenade or other explosive device, you should apply an explosive impact force to all rigidbodies around. Usually, this is done in the explosion script: the rocket suicides and instantiates an explosion when hitting something, and the explosion script applies the explosive force to all nearby colliders according to how close they are to the explosion:

Explosion script:

var radius = 5.0;
var power = 50.0;

function Start () {
  var explosionPos : Vector3 = transform.position;
  // find nearby colliders:
  var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
  for (var hit : Collider in colliders) {
    // if any of them have a rigidbody...
    if (hit && hit.rigidbody){
      // apply a ExplosionForce to it:
      hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);

NOTE: An interesting side effect of these strategies is that any rigidbody hit by the enemy shots will react properly, no matter if it’s a crate, a barrel, or whatever (provided that it has a rigidbody component, of course).

EDITED: I was too sleepy to notice that the push back force should be applied when the player collides with the enemy - maybe simply reversing the player velocity could do the job, like below:

public void takeDamage() { 
  // revert player velocity:
  rigidbody.velocity *= -1;