# Rotate around a pivot between two points?

How can I rotate an object like swing?Here is an example:
there are three points: A , B and O.O is pivot,now I want to rotate object from A to B ,then B to A ,and repeat this.
Thanks!

Since i’m more of a hackaround person, i’d make an emtpy object at exactly the same transformation as O.
Then parent to that the object i’d want to swing around O, open the animation panel and do a pendulum animation with O. Meaning, i’d rock O back and forth. That would cause the swing to … swing around O.

There’s also rotateAround() or something like that, but in your case you’d need to calculate the swinging motion and smooth it out before feeding it i think.

There are many ways to approach this problem depending on the details. Here is a simple(r) one. As indicated by @screenname_taken, if you put an empty game object at ‘O’ and child your object to ‘O’ you can rotate ‘O’, avoiding using the complexity of solving this problem with RotateAround().

Here is a script you can put on ‘O’. It requires you figure out the rotation. To do that:

• place an empty game object at ‘O’
• place your object at ‘A’
• child ‘A’ to ‘O’
• Now rotate ‘O’ until the object is at ‘B’
• Record the euler angles in the inspector and enter them for ‘endRotation’

Note start rotation should be (0,0,0)

``````#pragma strict
var startRotation : Quaternion;
var endRotation : Quaternion;
var speed = 15.0;

function Update() {
var t = (Mathf.Sin(Time.time * speed) + 1.0) / 2.0;
transform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
}
``````