public static float GetAngleFromVector(Vector3 vector)
{
float radians = Mathf.Atan2(vector.y, vector.x);
float degrees = radians * Mathf.Rad2Deg;
return degrees;
}
Mathf.Atan2(y, x)
x축을 기준으로 반시계 방향의 라디안(각도) 반환
범위 -π ~ π (-180도 ~ 180도)
참고로 Mathf.Atan 과의 차이는 파라미터 개수의 차이다 (atan - 1개, atan2 - 2개)
Mathf.Atan(y/x) 보다 Mathf.Atan2(y, x)가 적합한 이유: Atan은 ZeroDivisionError가 날 수 있기 때문
Mathf.Rad2Deg
Radian To Degree
라디안(radian)을 도(degree)로 변환하는 상수
1라디안(약 57.2958도)을 의미한다
Atan2으로 구해진 라디안 값에 Rad2Deg를 곱하면 도(degree) 단위의 각도를 구할 수 있다.
반대로 도를 라디안으로 변환하는 상수는
Mathf.Deg2Rad