- Home /
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! http://www.screencast.com/users/Lyon.Gu/folders/Default/media/75492834-eaeb-40b1-86da-934756fbff50
Is this like a pendulum where the swing should slow down on either end of the swing, or is like tick/tock where the speed is constant? I assume you are trying to do this with manipulating the transform, not by using a Rigidbody?
If you do this using three points, it means A and B will need to be at the exact same distance from O. Wouldn't it be easier if you used for instance two points and an angle ?
@robertbu ,hi,sir,you're AWESO$$anonymous$$E! You totally understand what I am saying! That is what I mean!Could you tell me any ideas?Thanks!
@$$anonymous$$aerig, hi,sir,you're also right!A and B have same distance from O. But sorry ,I am a beginner,I can't understand your advice :D Also thanks!
Answer by robertbu · May 13, 2014 at 02:39 PM
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);
}
@robertbu ,Thank you so much!You give me the right direction!I child my object to an GameObject,then I rotate the GameObject!Yahoo,I get what I want.Thanks!
Answer by screenname_taken · May 13, 2014 at 09:01 AM
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.
Thanks!I think you are right.I had used RotateAround.But I don't know how to reverse it from B to A,and repeat :D
If you are referring to the animation, do it like A goes to B, B goes back to A. Then set the animation as looping from the editor.
@screenname_taken ,Thanks! Nice solution!But I wish I could use my own code.Thanks for sharing!