- Home /
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?
Answer by Sazails · Nov 03, 2018 at 03:28 PM
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.
Hi @Armanis2017 ! Apologies for getting back to you so late - thanks a lot for your reply. I figured out the issue shortly after posting myself, and forget to post in on here.
The issue and the fix were:
In the FixedUpdate of the script, the part that handles player movement, the velocity was constantly being set. The AddForce Impulse was overwritten by whatever velocity that was being input in the movement part (in case the player wasn‘t moving - zero). I solved it by implementing a simple timer for the length of the knockback. That now allows me to control the knockback smoothly by setting the length and knockback amount.
Answer by ProjektSpark · Nov 03, 2018 at 03:22 PM
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 !
Hi @ProjektSpark ! In case you're still having this issue - I identified the problem in the meantime and was able fix it as follows:
In the FixedUpdate of the script, the part that handles player movement, the velocity was constantly being set. The AddForce Impulse was overwritten by whatever velocity that was being input in the movement part (in case the player wasn‘t moving - zero). I solved it by implementing a simple timer for the length of the knockback. That now allows me to control the knockback smoothly by setting the length and knockback amount.
Let me know if you need more help, then I could try to explain further or post my script.