- Home /
Does Unity come with a method that finds supplementary angles?
I am raycasting and hitting a slope, and using the returned RaycastHit2D
object I can use Vector2.Angle(raycastHit.normal, Vector2.up)
to find the slope's angle. Sometimes the slope will be considered greater than 90 degrees, and Vector2.Angle
will return something like 135 degrees. Is there a better way of finding the supplementary angle - which is 45 degrees in the aforementioned example - without hard coding something like float newAngle = 180 - slopeAngle
?
Essentially I would prefer that all calls to Vector2.Angle
return a float that is less than or equal to 90.
Answer by RedBambooLeaf · Mar 06, 2020 at 03:04 AM
I'm pretty sure Unity doesn't have such method... As an alternative, you can define an extension method which perform the operation you're looking for. This is very specific to this case, but it should give the idea:
public static class SomeStaticClass
{
public static float MyAngle(this ref Vector2 a, Vector2 b) => Vector2.Angle(a, b) % 90;
}
public class UsageExample
{
public void Operation()
{
// ... get your vectors
Vector2 a = Random.insideUnitCircle;
Vector2 b = Random.insideUnitCircle;
// ...
var angle = a.MyAngle(b);
}
}