- Home /
my child object of the earth object in Coursera Solar System project varies its position and speed when I play it
I have three objects in my sceneL sun, earth and moon, The sun rotates in relation to itself (spins) and the earth rotates in relation to itself and the sun using two scripts. The moon is a child object of the earth and rotates in relation to itself and to the earth using two scripts. However, the script to rotate around the earth is crazy. I set the suggested static position and speed and when I play it these numbers start changing wildly as the moon moves, It spins ok, but it does not rotate around the earth as far as I can see and it gets much too far from its parent. Does anyone know what I am doing wrong? I am a beginner at this using unity, but I have 25 years as a senior technical writer for software companies. Breaking things was always part of the job but I could identify the cause(s) and document all. Now I have no idea. HELP please.
Answer by nksG · Oct 03, 2016 at 08:13 PM
i guess that we would need to see the code to fully undestand the problem and propose solutions here.
Keep in mind, that if you rotate an object to itself(for example the Earth), all its children will rotate too,not to their axis,but to it's Parent(earth) axis.
Answer by andkarena · Oct 09, 2016 at 07:48 AM
Oh , so I only need to rotate the moon object to itself and around the earth, but it will follow the earth around the sun, hmmm guess one would cancel out the other and nmake the moon just circle empty space, Thanks, Tryoing to take all of this in as a chunk. Karena
Here is a simple sollution . $$anonymous$$ake an empty gameObject and attach the script to it. $$anonymous$$oon is child of Earth. Use the apropriate speeds for each rotation. Cheers :)
public GameObject Sun;
public GameObject Earth;
public GameObject $$anonymous$$oon;
// call PlanetOrbit inside the Update()
public void PlanetOrbit()
{
Earth.transform.RotateAround(Sun.transform.position, Vector3.up, 5);
$$anonymous$$oon.transform.RotateAround(Earth.transform.position, Vector3.up, 1);
Sun.transform.Rotate(0, 1, 0);
Earth.transform.Rotate(0, 1, 0);
$$anonymous$$oon.transform.Rotate(0, 1, 0);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612093912im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I need help with my character's rotation script 1 Answer
one gameobject postion to another gameobject rotation 0 Answers
RotateTowards in a specified direction 0 Answers
Billboard script restrictions 1 Answer
Rotate the shortest way? 6 Answers