loading words...

Apr 30, 2019 20:25:17

Unity - Rotate Around While Maintaining Direction

by @valentino | 262 words | 60🔥 | 358💌

Valentino Urbano

Current day streak: 60🔥
Total posts: 358💌
Total words: 170604 (682 pages 📄)

The best way to rotate around an object is to use the built in method RotateAround passing in the radius you want your circle to be. Using rotate around on an object to make it rotate around a different object doesn't automatically fix the direction your object is facing though. It will keep the object as it is and just move it to rotate around without preserving any of your axes.

The object will correctly rotate around the direction is facing won't be updated. If you just need to look at the object you are rotating around you can simply call LookAt() to look at it at that's it. If you need to face the direction you are moving towards (the forward direction) it is a bit more difficult.

After calling rotate around



var dir = -(center.position - transform.position);//you might not need the - in the beginning depending how your scene is setup

var angle = Mathf.Atan2(dir.y, dir.x) \* Mathf.Rad2Deg;

transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);//here you need to use the same axis you use in the rotate around method. If you use .forward use it here as well.


The last option is that you don't want to face anything at all, but keep the initial rotation that you had before starting to rotate around the object. In that case, you can save the object rotation before calling rotate around and set it back to the transform.rotation right after. You will keep rotating around the object while keeping the initial position intact.


var rotation = transform.rotation;

transform.RotateAround(center.position, Vector3.forward, rotationSpeed * Time.deltaTime);

transform.rotation = rotation;


Originally published at www.valentinourbano.com

contact: email - twitter / Terms / Privacy