- Home /
Orbit around orbiting object (transform.RotateAround)
The best way to explain this is I'm attempting to make a small model solar system. There's a sun, a planet, and that planet's moon. The planet orbits like normal, however the moon orbiting around the planet shoots off and makes an extremely large and far-away orbit.
My code is as follows:
public class RotateAndOrbit : MonoBehaviour {
public Transform target;
public float RotationSpeed = 100f;
public float OrbitDegrees = 1f;
void Update () {
transform.Rotate(Vector3.up, RotationSpeed * Time.deltaTime);
transform.RotateAround(target.position, Vector3.up, OrbitDegrees);
}
}
I'm not sure how to compensate for this or even what my mistake is called, but any help would be appreciated.
$$anonymous$$mm, not sure but try to call the fixedupdate function, this could solve the problem it u have cpu spikes
@miskoma, your answer worked exactly like you described it. Thanks for your help, it was a brilliant idea!
@son.of.lysander I converted that to an answer, than please tick as true
Answer by miskoma · Sep 10, 2013 at 08:40 AM
There's an alternative method, can give you just a hint. You can place your moon in a empty game object, place the EGO in the place of the planet and place the moon relatively in a place, where the EGO rotates around itself, the moon being a child will rotate around.. hope it helps
For the sake of making sure I give credit where it is due I chose this; HOWEVER, my final solution came from my duplicate question on stackexchange: (http://gamedev.stackexchange.com/questions/61981/unity3d-orbit-around-orbiting-object-transform-rota$$anonymous$$round). It doesn't require making any extra empty game objects.
Just placing the items withing each other as children will produce the desired result
Your answer
![](https://koobas.hobune.stream/wayback/20220613113907im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate around moves object out of position, why? 1 Answer
How to limit the angle of a camera with transform.RotateAround? 1 Answer
Rotating a camera around a sphere to look at a specific point 2 Answers
Get the rotation of a object around a arbitrary axis. 0 Answers
Rotate Around Planet with Spaceship Face Set on Path 1 Answer