Keep object within circle on plane
I'm trying to create an aiming system which creates a radius around an object and allows an aiming reticle to move around within that radius but does not leave the circle, as in the example picture below:
I've seen an example which looks like it keeps an item within a circle and I understand the concept (you check the distance of the object from the center and make sure it's within the radius), but I would like to get some help on translating this 2D concept to 3D.
I think it would act the same except along the X,Z planes since Y is vertical, is this correct?
This is my code:
private Transform mAimPosition;
public int Radius;
private float mDistance;
private Vector3 mCenterPosition;
public void MoveReticle(Vector2 delta) {
mAimPosition.Translate(delta.x, 0, delta.y);
mDistance = Vector3.Distance(mAimPosition.position, mCenterPosition);
if(mDistance > Radius) {
Vector3 fromOriginToObject = mAimPosition.position - mCenterPosition;
fromOriginToObject *= Radius / mDistance;
mAimPosition.position = mCenterPosition + fromOriginToObject;
}
}
I've looked at the following examples: https://answers.unity.com/questions/432472/how-to-create-a-circle-around-object-.html https://gamedev.stackexchange.com/questions/126427/draw-circle-around-gameobject-to-indicate-radius https://answers.unity.com/questions/1309521/how-to-keep-an-object-within-a-circlesphere-radius.html