Hi! I have a very complex problem. I need to calculate the length between two points (A and B) called “c”, after a moment’s thought I came up with the idea of doing a right triangle. I know the coordinates of point A and B, point C has the same x-coordinate as point A and the same z-coordinate as point B (all points have y = 1). on this basis I know the length “a” which is a = Xc-Xb (Xc-coordinate x of point C, Xb coordinate x of point B). c = a / sin α, I know how to calculate a, I know how to calculate the sine (Mathf.Sin ()), I don’t know how to calculate the angle α. And this is what I ask, how to calculate this angle? Or how other method to calculate the length “c”? Thank you in advance for your help P. S I’m not very good at English;)
This is not Unity specific but looks like a math homework. If thats the case you are at the wrong place. If it is Unity/game related and if those points are given you could just use Vector3.Distance to calculate their distance. Probably there is a 2d counterpart too when you work with a 2d project.
Basically all of what @exiguous said. If you really only want to know the length of ‘c’, then use Vector3.Distance. If you went through such lengths to include all those other thoughts… then it somewhat seems like you actually want to achieve something else?
The exact method to calculate c is known as Pythagoras’s theorem.
a^2 + b^2 = c^2
and from there c = sqrt(a^2 + b^2)
This is exactly what Vector2.Distance does.
Implementation-wise it would look like this
float Distance(Vector2 a, Vector2 b) {
var delta = b - a;
return Mathf.Sqrt(delta.x * delta.x + delta.y * delta.y);
}
It’s hardly a ‘complex’ problem though.