- Home /
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.
Answer by ahmedbenlakhdhar · Dec 02, 2014 at 01:38 AM
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 link.
Your answer
Follow this Question
Related Questions
Rigidbody not calling OnCollisionEnter 1 Answer
Rigidbody2D won't apply force after collision 0 Answers
jump script : 2D 1 Answer
AddForce vs Velocity issues with Rigidbody2D 2 Answers
C# OnCollisionEnter2D Not Occuring 1 Answer