- Home /
Rotating object between two points?
Basically I have two spheres with a cube between them. One sphere links to the other sphere using a "Spring joint" and moves around it (imagine a planet in orbit). What I want is the cube to stay between them and to rotate accordingly so it always faces the central sphere. How would I do this?
(i've tried making the cube a child of the sphere but it just messed it all up)
You could use another joint between the cube and the central sphere, and then use transform.LookAt to make sure it is always pointed in the right direction.
Answer by SinisterRainbow · Jun 14, 2013 at 05:33 AM
(edited for formatting reasons:) All you need to do is grab the position vector from sphere A and sphere B then add them and divide by 2 to get the position vector for the box. (pseudo code:), then have BOX.lookat(A) as e.bonneville suggested. There are some great tuts on Vector math to learn the basics of things like this, will make game programming much more fun and easy.
In other words:
GameObject sphereA, sphereB, BoxOne;
...
Vector3 midpoint = (sphereA.transform.position + sphereB.transform.position) / 2;
BoxOne.transform.position = midpoint;
Box.transform.LookAt(sphereA.transform);
Did this answer your question okay? If so, can you mark it answered, otherwise it remains in the unanswered questions pile.
@SinisterRainbow Sorry to resurrect this thread, but do you know how to do it via configurable joint?