0

How can I find the angle between two angles that go from -180 to 180 degrees?

I would like to do :

if (angleDiff > 90)
{
    // The angle is bigger than 90 degrees... 
} 

I tried subtracting them but that doesn't seem to work. Thank you

DMGregory
  • 134,153
  • 22
  • 242
  • 357
Trafel
  • 1
  • 2

1 Answers1

1

Unity has a built-in function for this:

public static float DeltaAngle(float current, float target);

Description


Calculates the shortest difference between two given angles given in degrees.

So you can write...

if ( Mathf.Abs(Mathf.DeltaAngle(angleA, angleB)) > 90f ) { ... }

Don't forget to search for past answers and read the documentation when you have questions about common tasks like this. It's almost guaranteed you're not the first one to try to solve this problem.

DMGregory
  • 134,153
  • 22
  • 242
  • 357