- Home /
rigidbody2d.addforce for is not working for the x axis
i made it that when my character gets hurt or hit by an enemy it gets pushed by a pushback force caused by the damage by using addforce. The only problem is that the addforce only works for the y axis not the x even though its supposed to work for both, hears the code.
if (pushback) {
GetComponent<Rigidbody2D>().AddForce( new Vector2(pushbackforcex,pushbackforcey),ForceMode2D.Impulse);
pushback = false;
}
Answer by plof27 · Jun 12, 2018 at 09:46 PM
I know I'm 3 years late on this, but I came across this post because I ran into the same problem. The solution for me was to uncheck Apply Root Motion
on the objects Animator
component. For some reason, this causes weird locking on some, but not all, axes even if there is no parent object int the hierarchy. Hopefully this helps any other lost souls who find themselves here years in the future.
Answer by Jackal14 · Jan 17, 2019 at 03:36 PM
I came across this error too and i think i found exactly, what the problem was, i'm not sure about disabling the animator because Apply Root Motion was already marked off in my project.
The exact reason for this problem is that your X axis component of RIgidbody is already being updated elsewhere at the same time when you are trying to create this KnockBack or Bounce. So check if there is any Conflict for X axis movement in the same script or if any other script is trying to access it. This Resolved it for me. Hope this helps!!
This worked for me! My movement of my player was using the rigid-body.x axis as thus every update frame it kept altering it.
Answer by kevinspawner · Aug 18, 2015 at 03:14 AM
From your one line code, I can only imagine swapping the pushbackforcex,pushbackforcey value to pushbackforcey,pushbackforcex. It would be easier to check the issue, if you can paste the full code.
don't worry i have solved it all i needed to do was add more force thank you