How to set a spherical limit on a camera movement on a moving and rotating object. (3d)

I am making a solar system based game where all the planets are constantly rotating, the problem is that I need a camera that follows a planet and allows for movement around the planet at a constant distance, while the planet is moving around the sun, and rotating allowing for orbiting moons.

I have tried a few movement limiting scripts, but they don’t work with a spherical object.

The only way i can see this achieved is with math… take a look at this, i think this is a good starting point