Question by
drorco · Jul 17, 2021 at 02:01 PM ·
2drigidbody2drigidbody.addforce
Rigidbody2D apply force is much stronger in left-right VS up-down
Hey everyone,
I got a simple sprite with a RigidBody2D applied to it. The issue I have is for adding equivalent force on keydown, for each arrow press, the force applied on left-right clicks compared to up-down is much much stronger.
Here's my code:
if(Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log(this.rigidBody.velocity);
this.rigidBody.AddForce(new Vector2(0, 1), ForceMode2D.Impulse);
}
else if(Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log(this.rigidBody.velocity);
this.rigidBody.AddForce(new Vector2(0, -1), ForceMode2D.Impulse);
}
else if(Input.GetKey(KeyCode.LeftArrow))
{
Debug.Log(this.rigidBody.velocity);
this.rigidBody.AddForce(new Vector2(-1, 0), ForceMode2D.Impulse);
}
else if(Input.GetKey(KeyCode.RightArrow))
{
Debug.Log(this.rigidBody.velocity);
this.rigidBody.AddForce(new Vector2(1, 0), ForceMode2D.Impulse);
}
It gets to a level in which for 5 clicks up I'm getting slight movement up, and 2 clicks right, I'm getting crazy movement to the right.
Any idea for why this happens?
Thanks!
Comment
Best Answer
Answer by drorco · Jul 18, 2021 at 12:19 PM
OK I figured it out.
My top-down buttons were set as GetKeyDown and my left-right buttons were set as GetKey, therefore applying continuous force.