- Home /
Getting cosinus of an angle in degrees
Hey, so I have an object in a scene rotated around the y-axis with an angle of 45 degrees. When I put this line of code in the script and run it:
print (Mathf.Cos (transform.rotation.y));
The editor returns the value '0.927...'. Is this the value in radians? because I checked and the cos of 45 degrees is 0.707. and if so, how can I get Unity to use 0.707 instead of 0.927?
and I also I thought the cos of 45 deg in rad was pi/4 rad and that is 0.78
Thank you very much!!!
Answer by meat5000 · Jan 23, 2015 at 07:44 PM
http://docs.unity3d.com/ScriptReference/Mathf.Rad2Deg.html
http://docs.unity3d.com/ScriptReference/Mathf.Deg2Rad.html
Degrees = (Radians x 180) / Pi
Radians = (Degrees x Pi) / 180
but still the cos of 45 degrees in rad isn't 0.9 right?
You can work this out by putting in the extreme values.
Try cos(90), cos(180) etc and see what the results are. The extremes should give answers of 0,1,-1 if you look at a graph.
(Cos(45) in degrees gives an answer of 0.707)
Answer by tanoshimi · Jan 23, 2015 at 07:53 PM
transform.rotation.y
is not what you think it is. It's the y component of the rotation expressed as a quaternion (which is basically black magic). What you're looking for is the rotation around the y axis as an angle expressed in degrees, which is:
transform.eulerAngles.y
Ok, this solved the issue of the value being weird, now they are what I would expect them to be in radians, I'll just need to convert them to degrees now, tyvm!!
Answer by Owen-Reynolds · Jan 23, 2015 at 08:02 PM
You almost never have to use cos/sin in Unity. If you back up to what you're trying to accomplish, there's probably a nice Unity built-in that will do it, w/o needing any math.
Misc problems: 1) rotation.y
is a (useless) Quaternion value. It isn't degrees or radians. transform.eulerAngles.y
will give the value you see in the Inspector for rotation. 2) transform angles are in degrees. Like all computer cos/sin functions, Unity Cos expects the input to be in Radians. 3) Radians have 0 facing right and go CCW, while Unity degrees have 0 forward and go CW. This sort of thing is common, and you'll probably find better info in (non-unity) trig forums.
But, really, if you are even asking if the value of cosin is in radians (it isn't in degrees or radians!) then avoid trig and use builtins. For example, transform.right
gives the length 1 right-facing arrow (the red X arrow in the Scene window.) So transform.right.x
will give a sort of cosine, if the object's Y-axis a straight up. But, you may not even need cosine to do what you really want.
I have a door object, and I need to place an object on runtime on the side of the door to function as a hinge on EVERY possible door. so position.x of the hinge = position.x of the door - scale.x of the door/2
I think I need cos to figure out where to place the hinge when the door is rotated
Easier to use transform.right*(scale.x/2)
, or to give each door-type an empty "mountPoint." There are a bunch of old answered Q's about positioning objects based on other objects.