Question by
PinguxUlt · Aug 10, 2019 at 07:08 AM ·
2d gameaddforceoncollisionenterknockback
How to make a knocback smooth - 2D Unity
This is my very first "game" in unity, first of all i want to apologize for my bad english. What i am trying to do is when my player hits a mob, he gets knockedbacked a few meters away, i did this using AddForce with vector2 using x and y axis, but what it does it's just teleporting my guy a few meters away, i want a smooth action. I tried to add ForceMode.Impulse or ForceMode.Force after the vector2 but it doesn't work either. Below is the code.
public int playerhealth = 100;
public Rigidbody2D rb;
public float knockback = 100;
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Mob")
{
Vector2 da = new Vector2(knockback, -knockback);
playerhealth -= 1;
rb.AddForce(da, ForceMode2D.Impulse);
}
}
Comment