How do I get ForceMode2D.Impulse to work?

Hi everyone!

I’m trying to apply an Impulse force to my Player Character to knock him back when he’s being hit by an enemy. I’m doing this inside a separate function inside my player controller that gets the direction for the knockback, as well as the amount of knockback from whatever object the player is attacked.

Now, if I don’t specify the ForceMode2D, it works fine. Albeit only when the float knockback is a ridiculously high number, like 10’000. This makes sense however, as in it’s normal mode, AddForce is applied over time, and as it’s only executed once it needs that high number. That however isn’t smooth at all, and as I understand it, that is where ForceMode2D.Impulse should come into play.

As soon as I set the ForceMode2D to Impulse however, it doesn’t work at all anymore.

Here’s the simple function:

	public void knockBack(Vector2 knockbackDirection, float knockback)
			rb2d.AddForce(knockbackDirection * knockback * Time.deltaTime, ForceMode2D.Impulse);

Like this it doesn’t move at all. Seemingly no force is being applied to the Rigidbody. As soon as I remove ForceMode2D.Impulse it works again as mentionned. The Vector 2 knockbackDirection and float knockback have correct values in them.

The Rigidbody2D of the object is set to dynamic, with a mass of 1.

What am I missing?

You really should tell us the knockbackDirection you are doing and also make sure not to use Time.deltaTime as it is not needed. Time.deltaTime should be used in functions such as Update() and FixedUpdate(). Also make sure your rigidbody2D doesn’t have any constraints on rotation or position.

To see impulse working , I’d prefer to turn gravity to 0 and then test it. By going to Edit > Project Settings > Physics 2D and change Gravity from -9.81 on the y to 0.

Damn, this is killing me too. Keeping an eye out here if someon ehas a solution !,Damn this is killing me too.
Keeping an eye out here if someone has a solution !