- Home /
Question by
calvintmoss · Sep 07, 2014 at 03:48 AM ·
transformrigidbody2dvelocityrotatearoundrotatetowards
Rotate RigidBody 2D with respect to velocity
I am trying to make my spaceship rotate depending on its velocity as it orbits a planet. I am running into some issues however. I found this answer that showed how to do it but im not sure if its my setup or the code the needs tweaking. If I go around the planet counter clockwise, it works 100% as intended, but if I go clockwise instead of rotating toward the planet I rotate away from it. Does Anyone have any ideas?
For The lazy here is the script
var dir = rigidbody2D.velocity;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
var q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 18f * Time.deltaTime);
My player is a quad mesh with a 2d sprite pasted on it. I have taken a screenshot of its setup here.
The gravity is handled by another script and works great, if I ignore the visuals my object does everything correctly, and can go into orbit.
Comment