Calculating a right triangle trouble...

Update--4/1/11

Just running Mathf.Cos(45) returns 0.525322, when WebMath and my desktop calculator say its suppose to be 0.70710.

Debug.Log(Mathf.Cos(45));

Simplified code

var Point_A:Vector2 = Vector2(0,0);
var Point_B:Vector2 = Vector2(5,5);
var dir_a_to_b:Vector2 = (Point_B - Point_A);
var Angle_A:float = Vector2.Angle(dir_a_to_b, Vector2(1,0));
var Angle_C:float = 90.0;
var hypo:float = Vector2.Distance(Point_A, Point_B);
var adj:float = hypo * Mathf.Cos(Angle_A);
Debug.Log(Mathf.Cos(Angle_A));

I'm trying to calculate a right angle triangle but my math keeps coming back wrong.

//this cube sits at (0,5,0) but everything should be calculated on the x and z axis...so (0,0)
Cube_From = GameObject.Find("Cube_From");
Cube_From.renderer.material.color = Color.white;
//this cube sits at (0,5,5) or (0,5). Its 5 units down Cube_From's axis and is blue...so (0,5)
Cube_Z = GameObject.Find("Cube_Z");
Cube_Z.renderer.material.color = Color.blue;
//the angle between Cube_From to Cube_Z which returns 0.
CF_CZ = Vector2.Angle((Vector2(Cube_Z.transform.position.x,Cube_Z.transform.position.z) - Vector2(Cube_From.transform.position.x,Cube_From.transform.position.z)), Cube_From.transform.up);
//this cube sits at (5,5,5) or (5x,5z)
Cube_XYZ = GameObject.Find("Cube_XYZ");
Cube_XYZ.renderer.material.color = Color.yellow;
//get the angle of the opposite...should be 45
CF_CXYZ = Vector2.Angle((Vector2(Cube_XYZ.transform.position.x,Cube_XYZ.transform.position.z) - Vector2(Cube_From.transform.position.x,Cube_From.transform.position.z)), Cube_From.transform.up);
//get the lenght of the hypotenuse
hypo = Vector2.Distance(Vector2(Cube_From.transform.position.x,Cube_From.transform.position.z), (Vector2(Cube_XYZ.transform.position.x,Cube_XYZ.transform.position.z)));
//get the length of side_a
Side_A = Vector2.Distance(Vector2(Cube_From.transform.position.x,Cube_From.transform.position.z), (Vector2(Cube_Z.transform.position.x,Cube_Z.transform.position.z)));
//Calculate the adjacent/side a
adj = hypo * Mathf.Cos(CF_CXYZ);

My math is really rusty so I'm not sure if the calculation is simplified correctly.

Wikipedia says Cos A = adj/hypo and WebMath says its simplified to adj = hypo*Cos(A) but i don't get 5 when unity calculates it. I get 3.7 ish in length.

cos(45 degrees) is 0.707. cos(45 radians) is 0.52.

Unity thinks that 0 degrees is North (or forward) and goes clock-wise, so 90 degrees is right/east and 180 is south. Most game engines do the same.

Real math (Mathf.Sin and Cos) thinks that 0 is right and goes counter clockwise using radians, so 1.57 is North and 3.14 is left/west.

If you have an angle, A, you got from Unity (say rotation.euler.y is 10 degrees, meaning ahead and a little right,) you can convert to the "real" system using, ummm, `A2 = A*(2*Mathf.PI/360)*-1+Mathf.PI/2`

You can then use A2 (which is radians, CCW, 0=right) for trig functions. The results will be in distance, so don't need to be converted. Contrary-wise, if you use arcTangent for anything, it gives you the angle in "real" math radians, so needs to be converted back to gameEngine units with the opposite equation.

Trig functions are for 2D. What you want to play with are vectors. You'll need to know things like the cross product to get the adjacent side. Also, reflection angle and all to get the correct normal.

You could always use raycasting. It returns the normal, which would be the direction where you would want the cube to go.