- Home /
Question by
ShroomWasTaken · Feb 24, 2016 at 09:38 PM ·
movementrigidbody2dphysics2d
Simplest way to deaccelerate a Rigidbody2D object
I would like to know the simplest way to deaccelerate a object with a Rigibody2D
component in unity 5.
I would like it to deaccelerate the object's velocity from positive/negative values down to 0.
And I would like it to stop deaccelerating if a certain condition is true.
Example pseudo code:
if (Move-Right-Button.isPressed)
{
Deaccelerate();
}
if (someCondition)
{
Stop-Deacceleration();
}
Also, might be worth knowing that I use Rigidbody2D.AddForce()
to move my object.
And last thing, I would prefer code to be in C# if possible :)
Hopefully this made sense, my explaining and English skills aren't very good... :P
Comment
Best Answer
Answer by Tuncer · Feb 25, 2016 at 08:52 AM
You can do it easily :
float deaccelerationAmout = 10.0f;
void Update() {
if (Input.GetKey(KeyCode.RightArrow) && yourCondition){
Vector3 oppositeDirection = gameObject.GetComponent<Rigidbody2D>().velocity * -1;
gameObject.GetComponent<Rigidbody2D>().AddRelativeForce (oppositeDirection * deaccelerationAmout);
}
}
Ah of course... Not sure why I didn't think of this lol