- Home /
Calculating relative angles from Vector3's
So, I have a little problem in my hands. I need to calculate an angle based on only Vector3 coordinates. I already tried using Vector3.Angle, but that's not giving me the result I want to achieve, as the angle is not the angle related to the other vertices of the shape, but rather relative to somewhere else. I'm not really too good at writing out what I'm trying to do, but if anyone has any idea about what I should do, I can try to give more precise information of my project.
Here's a little example:
So, any help appreciated :) It might be an easy thing to do, but I really have no clue about it.
Answer by Harinezumi · Apr 13, 2018 at 08:34 AM
I think if you subtract the reference point from the other 2 points (basically moving them into its system of reference), then call Vector3.Angle()
on those vectors, you will get the desired result.
Right, if we call the left point "A", the right top point "B" and the lower right point "C" it's simply
float angle = Vector3.Angle(B-A, C-A);
"B - A" gives you the vector from A to B
Amazing! This works wonders. Now I'm just thinking: Is there a way to see if the angle exceeds 180°, and then do other stuff based on that? Because Vector3.Angle practically never gives a result above 179.9°. I would like it to be so, because in my project I have editable shapes like triangles, hexagons, etc. and I need to have it like so it never lets an angle to be over 180°, so it should never be a reflex angle. If that's not possible, it's not a big issue, just wondering whether I could somehow do it.
Just use Vector3.SignedAngle ins$$anonymous$$d of Vector3.Angle. Note that you have to provide an additional axis vector for reference. In your case either Vector3.forward
or -Vector3.forward
:
float angle = Vector3.SignedAngle(B-A, C-A, Vector3.forward);
It says that "UnityEngine.Vector3 does not contain definition for SignedAngle". What am I doing wrong? I did the same as in your example.
Your answer
Follow this Question
Related Questions
Average of Vectors [Math] 1 Answer
Rotate 3d vector based on local forward 1 Answer
Make something move away from click point. 2 Answers
Calculating endpoints of directional vector 2 Answers
Align line of units based on vectors 0 Answers