- Home /
Knockback not working
Hi, I have tried all solutions offered on similar questions, but nothing seems to work. Been trying to accomplish the seemingly minor task of an effective knockback for about an hour. Through Debug.Log, I know the collision registers, but somehow, the enemies simply push the player character around instead of applying knockback. Here is my code:
void OnCollisionEnter2D (Collision2D other)
{
if (other.gameObject.tag == "Player") {
other.gameObject.GetComponent<Player> ();
if (Player.invincible == 0 && charm == false) {
Vector2 dir = other.contacts [0].point - (Vector2)transform.position;
dir = -dir.normalized;
Debug.Log (dir);
Player.rb2d.AddForce (dir * knockbackForce);
Player.OnHit ();
}
}
}
Any help very appreciated :)
Answer by tormentoarmagedoom · Sep 14, 2017 at 01:47 PM
Hello!
I'm not used to use physics in my projects, but is possible the AddForce needs (Vector2 , Force.mode) and not (Vector2 * Force.mode) ?
Hey, thanks for the suggestion :) I should probably have been more clear on the matter, but "knockbackForce" is not a Force.mode (I tried using impulse, and it made no difference), it's a variable I use to adjust the magnitude of the knockback. Pretty new to the physics system myself though, so I might have misunderstood something
Your answer
Follow this Question
Related Questions
2D Top down shooter, gun is shaking while moving and Cursor solutions 0 Answers
Touch Buttons 0 Answers
How to make Enemy shoot at Player Top Down 1 Answer
how to 2D top down 0 Answers