RigidBody2D - AddForce - KnockBack Effect.

Hello everyone.

Im trying to create a knockback effect.

So, I have 2 gameObjects.

1)Player
2)Ball

What I want to accomplish is to randomly create a ball that moves towards the player, when hit, the player must move “back” from where it was hit.(As like following the path of the ball).

What I was trying was :

void OnCollisionEnter2D(Collision2D other) {
		Debug.LogWarning ("Player Collision");
		
			
			if (other.gameObject.tag.Equals("Ball"))
			{
				_playerRigidB.AddForce(other.rigidbody.velocity * 1);
				Debug.Log ("Pushing");
			}
		
			Debug.Log ("done");
	} 

But it doesn’t seem to be working. Am I doing this correctly?

Thanks for your time.

Create a Physics Material 2D, increase its Bounciness, and then add it to Ball game object as a component. This way, Ball will bounce when it collides.

For more informations about Physics Material 2D, check this 1.