I’m having a little trouble with my code. I’m using the Mathf.Acos function but it only returns Nan. Whats the issue?
Heres my code:
AB = 1.5f;
BC = 2f;
AC = 3f;
float angleB = (Mathf.Pow(AB, 2) + Mathf.Pow(BC, 2) - Mathf.Pow(AC, 2)) / 2 * AB * BC;
angleB = Mathf.Acos(angleB) * Mathf.Rad2Deg;
Debug.Log(angleB);
Acos, by definition, will return a NaN value whenever the input is below -1 or above 1. It’s enough when you are a tiny bit below / above. So just make sure the value you pass to Acos is in the proper range. That’s why most methods that use Acos will clamp the value. Like the Angle method.
ps: Note that this part:
/ 2 * AB * BC;
Does NOT divide by AB and BC as division and multiplication are on the same level and are evaluated left to right. So you only divide by 2 here. You actually multiply by AB and BC which you probably didn’t want to do. Either use division 3 times, or put brackets
/ (2 * AB * BC);
4 Likes