- Home /
Mathf to get point on edge of circle (via radius and angle)
Hi guys, I am trying to create a GUI wheel with power-ups icons and having some trouble. I researched this quite a bit, and what I want it to do is put one gui element an equidistant space from the center of a circle area (thus creating a "power wheel"). However they are appearing unevenly spaced. I checked with some Debugs and it seems like the 360 degree circle is being split evenly according to the number of elements. Can you guys take a look at the code and point probable cause out? Thanks!
Debug.Log(angle);
rad = Screen.height * .00017f;
for(int i = 0; i < powerL.Length; i++){
//Debug.Log(angle);
tweenObject[i] = (GameObject)Instantiate(Resources.Load("3dText"));
tweenObject[i].transform.name = powerL[i];
tweenObject[i].transform.parent = spawnPoint.transform;
tweenObject[i].transform.position = new Vector3(.15f,.4f,.01f);
float X = tweenObject[i].transform.position.x + rad * Mathf.Cos(angle);
float Y = tweenObject[i].transform.position.y + rad * Mathf.Sin(angle);
Vector3 finalPos = new Vector3(X,Y, 0);
//Vector2 finalPos = new Vector3(X,Y, 16.2785f);
iTween.MoveTo(tweenObject[i].gameObject,finalPos, .5f);
angle = angle + (360 / powerL.Length);
Debug.Log(angle);
}
if you did this with GUI elements, it would be so much easier :P
also cos and sin take an angle in radians, not degrees
GUITextures can be simpler in that they are already in camera space -- you don't have to keep them in front of a moving camera.
But, if the power-ups will be rotating 3D models, can keep them as 3D transforms and use a second camera (adjusting layers and viewport.) The text can then be 3DText objects.
Answer by Owen-Reynolds · Mar 21, 2013 at 10:11 PM
As BenPod comments, Cos and Sin expect radians. 6.28 radians is 360 degrees. So if you use 45 "degrees", the computer spins 7 full circles and looks at the remainder, which is essentially random (which way is 12,060 degrees? After subtracting all the 360's, could be anything.)
To fix, use Mathf.Cos(angle* Mathf.Deg2Rad );
. Deg2Rad is just 6.28/360, about 57.
It's actually about 0.0174 ;) 57 is the multiplicative inverse. That's the value of Rad2Deg. Anyways, correct answer ;)
Thanks guys. I appreciate the help. I will look into GUI elements again. (I believe I tried it at one point)
Your answer
Follow this Question
Related Questions
Checking Attack Radius, or "Width" 1 Answer
iTween movetopath 1 Answer
Go back to first node (itween + moveto) 2 Answers
itween moveto not working 1 Answer
Moving a cylinder connecting two spheres 0 Answers