- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
EVOin3D · Jan 13, 2013 at 10:17 AM ·
transformrotaterotatearound
RotateAround help
I'm trying to create a moon/planet/sun orbit of sorts but am having some trouble. Here's what I have so far:
{
public Transform planet;
public Transform moon;
void Update ()
{
planet.transform.RotateAround(Vector3.zero, Vector3.up, 5 * Time.deltaTime);
moon.transform.RotateAround(planet.position, Vector3.forward, 90 * Time.deltaTime);
}
}
I have the moon parented to the planet, so it orbits the sun ok. However, it orbits the planet on one axis like this:
As you can see it looks ok on the left and right, but as the moon approaches the top and bottom it's still orbiting the planet on the same axis, making its orbit wonky. How would I go about coding it so that the moon is always facing the sun?
screen shot 2013-01-13 at 6.56.07 pm.png
(11.3 kB)
Comment
Answer by darthbator · Jan 13, 2013 at 09:28 PM
Not an elegant solution but you can put a script on the moon that says something like this.
public Transform sun //drag it or get it in start, whatever works! :)
void Update () {
transform.LookAt(sun.position);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613090847im_/https://answers.unity.com/themes/thub/images/avi.jpg)