# Clamping a Transform into Orbit

I’m pretty new to Unity (and scripting in general), and I’m having trouble figuring out something that’s probably very simple:

In C#:

I’m setting up a camera script to orbit a lookat target at a set distance.
Because of some other things I’d like to do, I want to handle the orbiting by giving the script a GoTo transform, rather than by rotating the camera. To keep things simple, I’d like to have the script somehow clamp the GoTo transform to make sure it stays in orbit (at the set distance) from the target.

Basically This:

Current position:

GoTo position:

GoTo position is then clamped to guarantee it’s at the set distance:

But I’m having trouble figuring out exactly how to do the clamping in code. Seems like there could be a common solution to this- Anyone know a good method?

Thanks for any help or advice-

You should probably google for some Vector math tutorials, this kind of stuff is dead simple once you have a decent grasp of vectors (it only takes a few hours of reading to get the hang of it).

To do this you’d get the vector that would translate from the centre of the orbit to the new position, which would be (5, 0, -5) if the object you want to orbit around is located at the world origin of (0,0,0). Normalise it, then multiply by orbit distance and you’re done.

Transform orbitTarget;
float orbitDistance;

this.transform.position = (this.transform.position - orbitTarget.position).normalized * orbitDistance;