- Home /
This question was
closed Oct 15, 2020 at 11:42 PM by
IsiahDaBoss67 for the following reason:
Other
Question by
IsiahDaBoss67 · Jul 14, 2020 at 06:49 PM ·
unity 2drigidbody2daddforce
How to set rigidbody force
Is there a way to set the force on an object in rigidbody2d, currently I'm using this but the speed constantly accelerates and decelerates.
void FixedUpdate()
{
if (direction.magnitude != 0)
{
rb.AddForce(direction / 10, ForceMode2D.Impulse);
}
else
{
rb.velocity = Vector2.zero;
}
}
Comment
Answer by Z_Y_X · Jul 14, 2020 at 07:13 PM
OK, as I understand you do not need adding force, you want to add velocity:
rb.velocity = direction / 10;
if you want to add force, do it once not in FixedUpdate, for example in OnCollisionEnter or :
if(Input.GetKeyDown(KeyCode.W))
{
rb.AddForce(direction / 10, ForceMode2D.Impulse);
}
The problem is that I want the character to move at one consistent speed because I'm using add force it keeps adding up and gets continuously faster