rotatearound + independent bodies

I looked around and couldn’t find anything that seems to fix this, so apologies if I missed something.

I have two spherical bodies (let’s say Earth and the Moon); Earth has an axial tilt + rotation, Moon has an axial tilt and rotates around the earth. When the two of them are stationary in the worldspace, everything is fine.

Now I add movement through worldspace to the Earth; the Moon continues to rotate, but at the Earth’s original position, not its current position.

Here’s my code:

var Target:Transform;
var Speed:float = 0;

function Update()
{
	if (Target)
	{
		if (Speed != 0)
			transform.RotateAround(Target.position, transform.up, Time.deltaTime * Speed);

	}

Am I doing something wrong? And/or how do I get the Moon to move with the Earth as the latter moves through worldspace?

Thanks!

I wouldn’t do this with the rotate around function, I’d make a new gameobject, and then child the moon to this new gameobject, and offset the moon so many units to side of the center of this new parent gameobject. So that the moon will orbit around this empty gameobject when the empty game object is rotated.

Each frame I would then set the empty game objects position equal to the position of the earth, then just rotate the empty gameobject with a simple eulerAngle.y += Time.deltaTime

Thanks, that works easier and more cleanly. :smile: