# Moving to a Point on a Circle

This topic is 2249 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all, been working on this issue for a little bit now and I seem to have hit a road block, it just isn't doing what I'm expecting it to do.

What I'm trying to accomplish is making the Cam move to a point a MaxDistance away from the Selected object. I don't understand why my current code isn't working, are my angles incorrect? What am I overlooking? I attached an image to help clarify what I'm trying to do.
[attachment=7912:MoveCamToPoint.png]
Vector3 CalculateNewMoveToPosition(Vector3 Cam, Vector3 Selected, float MaxDistance) { Vector3 calculated = Vector3.zero; float angle_between = 0; float angle_mod = 0; float dx = 0; float dz = 0; if (Cam.x >= Selected.x) dx = Cam.x - Selected.x; else dx = Selected.x - Cam.x; if (Cam.z >= Selected.z) dz = Cam.z - Selected.z; else dz = Selected.z - Cam.z; if (Cam.x < Selected.x) angle_mod = Mathf.PI; if (Cam.z < Selected.z) angle_mod = -Mathf.PI; if (Cam.x > Selected.x && Cam.z < Selected.z) angle_mod = 2*Mathf.PI; angle_between = Mathf.Rad2Deg * (Mathf.Abs(angle_mod - Mathf.Atan(dz / dx))); Debug.Log(angle_between); calculated.x = Selected.x + (MaxDistance * Mathf.Cos(angle_between)); calculated.z = Selected.z + (MaxDistance * Mathf.Sin(angle_between)); calculated.y = Cam.y; return calculated; }

Any help would be greatly appreciated. Thank you very much.

##### Share on other sites
Uhm... maybe I'm missing something, but what exactly would be wrong with not putting up with any angles at all? There seems to be an unhealthy obsession with trigonometry where nobody needs it. (Also, all those confusing if's make my head hurt.. math for the most part doesn't generally doesn't work that way).

Look at what you're doing. You already KNOW the vector and instead of simply scaling it to the right length, you bash the problem with the trig club to calculate angles you then use to calculate a shorter version of that same vector. That is simply overcomplicating the problem with a pointless detour.

 const Vector3 directionToCam = (Cam - Selected).normalize(); return Selected + (directionToCam * MaxDistance); //If you really want to be on a circle (not a sphere): //However keep in mind that if you move it to that position, you will no longer be centered on the object. Vector3 directionToCam(Cam.x - Selected.x, 0, Cam.z - Selected.z); Vector3 Calculated = Selected + (directionToCam.normalize() * MaxDistance); Calculated.y = Cam.y; return Calculated; 

##### Share on other sites
Ah yea, I agree. Thanks for that, I need to start using Vector algebra more.

On a side note, I just got the original code working... I never needed to convert to degrees. /facepalm

Haha,

Thank you.

• 9
• 23
• 10
• 19