Unit Circle Equation Question!

Hey there!

So I’m working with a GUI script that needs to allow someone to drag the cursor by holding down the mouse button, but only to a certain distance away from where they started holding down that mouse button. After that point, the cursor should stay in the circle–but at an angle that’s appropriate to where the cursor is outside of the circle. It’s sort of the way that Angry Birds allows you to drag the bird a certain distance away from the slingshot, but then keeps the bird at the correct angle, even if you’re dragging it farther than the slingshot will allow. So, I’m calculating the angle that the cursor is from the center of the circle correctly (i.e. 360 and 0 are when you’re directly “above” it in the GUI, and 180 is below, and so on. However, when the cursor attempts to calculate its position, it’s moving way too quickly around the circle. I think it has to do with these two lines of code. Do you guys know what I’m doing wrong, calculating the psoitions of the cursor? I realize this question is kind of vague, and perhaps hard to understand. But I’ve made a video of what’s going on, so you can see what I mean…here’s the code:

circleCoordinates.x =  (dragPlaneRadius * (Mathf.Cos(angleFromOrigin)) + xOffset;
circleCoordinates.y =  (dragPlaneRadius * (Mathf.Sin(angleFromOrigin)) + yOffset;

and here’s the video:

Ok then. Assuming that you know the true position of the mouse, and the point at which you want the cursor to rotate, first you get the original offset:

Vector2 originalOffset = mousePosition - rotateAroundThis;

Then you use this handy function called Atan2-

float angle = Atan2(originalOffset);

Then use that and your desired radius to reconstruct the correct location.

 float correctRadius = Mathf.Clamp(0, desiredRadius, originalOffset.magnitude);

Vector2 correctOffset = new Vector2(correctRadius * Mathf.Cos(angle), correctRadius * Mathf.Sin(angle));

Then use the correctOffset to draw your cursor!