- Home /
calculus help angels of a triangle
I have to calculate the angle of something (my camera) in my program. the only problem is I don't know how to implement the equation.
I need to use Mathf.Atan(A); to implement this formula:
but I don't know how to do it. what would the code look like?
here's the data I'm using:
ADJACENT = Camera.main.transform.position.z
OPPOSITE = 2.761007
Here's my guess at what the code would look like but I really have no idea:
myAngle = Mathf.Atan(2.761007 / Camera.main.transform.position.z);
but that just gives some decimal so I don't know. please help me :)
ps. this is calculating an angle on a right triangle. if you know of an easier way I'm all ears!
For very small angles you can approximate this by skipping the atan conversion: myApproxAngle=2.761007/Camera.main.transform.position.z*$$anonymous$$athf.Rad2Deg, which is slightly faster, but probably not worth the error, even when evaluating the atan every frame. The error is around 1% for 10 degrees, and 10% for 30 degrees.
Answer by Wolfram · Aug 06, 2010 at 10:48 PM
The Mathf trigonometric functions use radians, while Unity uses degrees. So you can use this to get an angle in degrees:
myAngle = Mathf.Atan(2.761007 / Camera.main.transform.position.z)*Mathf.Rad2Deg;