- Home /
Question by
mikelantzelo · Nov 17, 2020 at 09:17 AM ·
rotationrigidbodyvelocity
Rotation to rigidBody velocity 3D
Hi I have a cylinder with default rotation and a moving rigidBody. How can i align the direction of cylinder to rb velocity?
Comment
Best Answer
Answer by LadyOfTheMoon · Nov 17, 2020 at 11:43 AM
You can make a rigidbody face the direction it's currently traveling in by doing this:
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.rotation = Quaternion.LookRotation(rb.velocity);
}
However, this does assume that your cylinder is facing forwards by default, if not you'll want to adjust it. For example if you're using the default cylinder, which faces up, you could do this:
rb.rotation = Quaternion.LookRotation(rb.velocity) * Quaternion.Euler(90, 0, 0);