Rigidbody2D AddForce Impulse not working on X axis
Hi guys,
Can't figure out what I'm doing wrong. When my player hits a wall I want him to bounce back so I execute this code: [code=CSharp]Debug.Log("You hit a wall. " + new Vector2(direction xKnockback, yKnockback)); gameObject.transform.parent.GetComponent().AddForce(new Vector2(direction xKnockback, yKnockback), ForceMode2D.Impulse);[/code]
Now the strange part is, in the Y is working, the player is getting sky rocketted because I set the x and y knockback values very high, So I know it's working for the Y axis. But in the X direction it's just standing still. What could I be doing wrong?
Thanks for the help!
Answer by hnrqndrd · May 18, 2017 at 07:29 AM
Did you find out? :( I also have this issue.
I did not find a solution and my program got halted :( maybe it will start up again. If I find a solution I'll let you know. Please do the same. Good luck
@Xercium I just solved it. This link helped me.
What I basically did was removing the game object manually in the editor. Then I recreated it from the scratch (nothing too complicated, only 2D collider, rigidbody and animator) and added the same script. So yes, it is a bug in Unity (currently using 5.6.1f1).
Thank you for sharing mate!
I will test it out as soon as I can!
Answer by Jackal14 · Jan 17, 2019 at 03:34 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!!
And use Impulse instead of Force For ForceMode2D
Answer by plof27 · Jun 12, 2018 at 09:50 PM
I know I'm 8 years late on this, but I came across this post because I ran into the same problem. There was a thread here which actually had a functional solution. While remaking the game object can fix the problem, the real thing you need to do is uncheck Apply Root Motion
on the object's Animator
component. For some reason, this causes weird locking on some, but not all, axes even if there is no parent object in the hierarchy. Hopefully this helps any other lost souls who find themselves here years in the future.
This is some weird behavior. If I pick up my project again I will test it out. I hope it helps some people out! Tnx for you solution.
I had to login and thank you! so, THAN$$anonymous$$ YOU! :)
Answer by Joe_27 · Feb 03, 2019 at 11:32 PM
I'm having this problem too. But none of the above helped.
It's not the animator. And nothing else is updating my rigidbody as far as I can tell. AddForce simply doesn't affect the X-axis at all, while the Y-axis works normally.,I'm completely stumped by this problem with AddForce and the X-axis...
My X axis completely ignores AddForce, but my Y axis is completely normal. I'm at my wits end with this problem. None of the previous comments solved this problem.
This is everything involving my horizontal movement script. It works just fine and moves my character normally.
float h = Input.GetAxisRaw("Horizontal");
if (h * GetComponent<Rigidbody2D>().velocity.x < maxSpeed)
GetComponent<Rigidbody2D>().AddForce(Vector2.right * h * moveForce);
if (Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x) > maxSpeed)
GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Sign(GetComponent<Rigidbody2D>().velocity.x) * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
And this example command will affect my Y Axis but does absolutely nothing to my X axis no matter how ridiculous the Force, like the 999 in the example, but even something like 1 or 2 will make my character "jump" along the Y. While even the maximum value possible won't move my character at all.
if (Input.GetButtonDown("Dash"))
{
rb.AddForce(new Vector2(999, 0), ForceMode2D.Impulse);
}
Answer by AustinSyu · Apr 25, 2019 at 01:15 AM
Same issue, can't use addforce and root motion to affect the x axis at the same time. "Recreate a object" doesn't work for me.
My solution: Disable the animator before addforce than after a certain time enable it again. This is OK for my project but maybe not for yours...
Not sure what they are doing leaving such a ridiculous bug for years! But sure about that after some more little games, I'd better move on to learn another game engine.