bool IsAngleBetween(float angleToCheck, float angle1, float angle2)
{
// All angle parameters have been generated using Vector3.Angle followed by Vector3.Cross (to obtain sign of angle).
return … // Need this code here…
}
Head has hurt trying to work this one out…would love some help.
You might be able to use Mathf.DeltaAngle() for this.
However, it’s likely you could avoid dealing with angles and/or the periodicity issue entirely by using a different approach. What problem is it exactly that you’re trying to solve?
Although that information is useful, I think what would be most useful is if you could specify why you need to perform this test. For AI? Field-of-view test? Something else? With that information available, it’ll be easier to point you in the right direction.
Oh I see what are you asking. It’s because I allow a user to drag a particular game-object with the mouse. User clicks mouse button, I record the position of said object…user drags object, user releases mouse button and using the end position of object I can calculate this vector. Note the game is in 2d, all objects are dragged along the x/z plane only.
I need to see if the user has dragged this object within an pre-defined angle range from where it was first dragged.
Although I don’t completely understand the specifications, you should be able to figure this out by computing signed angles as needed. Although there are slightly more straightforward ways to do it in 2-d, an easy way to compute the signed angle between two vectors in this context would be to use Unity’s ‘unsigned angle’ function, and then determine the sign by taking the dot product of the global up axis and the cross product of the two input vectors.
Hi again Jesse, I was already acquiring signed angles, using exactly the method you describe. In fact, your previous suggestion of looking into Mathf.DeltaAngle was the clue I was looking for.
Put my thinking cap on and came up with the following solution:
bool IsAngleBetween(float angleToCheck, float angle1, float angle2)
{
// All angle parameters are signed (have been generated using Vector3.Angle followed by Vector3.Cross).