- Home /
Question by
Aladine · Oct 03, 2014 at 07:11 PM ·
rotationrigidbody2dvelocity
How to set Rigidbody2D velocity based on rotation
Hello everyone,
i want to find a way to replace this line :
//trans is the transform instance
trans.Translate (Vector3.up * flySpeed * Time.deltaTime);
The reason why i want to do this is because with that code the object act weirdly when it collides with something in front (instead of just stopping it tries to keep pushing giving a weird shaking effect)
The first thing i found is using making something like this :
rigidbody2D.AddRelativeForce (Vector2.up * flySpeed);
It kinda works but it keeps adding force to the object which after few seconds the object speed become ridiculously high
So i think what i need to do is to figure out the velocity of the object based on its rotation, knowing that this is how am controlling the object rotation :
if (Input.GetKey (KeyCode.LeftArrow)) {
euler.z += rotationSpeed * Time.deltaTime;
} else if (Input.GetKey (KeyCode.RightArrow)) {
euler.z -= rotationSpeed * Time.deltaTime;
}
trans.eulerAngles = euler;
Thank you
Comment
Best Answer
Answer by Aladine · Oct 03, 2014 at 07:51 PM
Okay this did the job :
rigidbody2D.AddRelativeForce (Vector2.up * flySpeed);
if (rigidbody2D.velocity.magnitude > flySpeed) {
rigidbody2D.velocity = rigidbody2D.velocity.normalized * flySpeed;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613164334im_/https://answers.unity.com/themes/thub/images/avi.jpg)