AddForce not working properly since upgrading to Unity 2018

I have these lines of code to knock back an enemy when you hit them:

rb.AddForce(knockBackForce);

It worked fine in Unity 4.7 but after updating the project to Unity 2018, the horizontal velocity stayed at zero so the enemy moves upwards before falling back down under gravity but it doesn’t move backwards. I don’t know what caused this or how to fix it.

@Parms

Add force has worked fine for me, it must be something else in your code.

And based on only those two likes of code it is pretty much impossible to say what is going on and what else might cause that - so maybe post your code.

“Rigidbody2D.AddForce” is a basic function of the 2D physics engine (Box2D) and we just provide a thin wrapper around it. It just modifies the velocity during the time-step and hasn’t changed at all since it was first implemented. I can understand how it’s easy to blame the engine but for such a basic and well used function it’s not very likely in this case.

I can only assume before you came to this conclusion you’ve debugged your code and verified that your “knockBackForce” is what you expect? Also, you’ve created a blank project and applied a force to verify that it’s not working and discount something else in your code?

If you can reproduce this issue then post the code that can duplicate it or better still, upload a simple repro project and I’d be happy to look at it for you.

1 Like