[Unity]CompareApproximately (SqrMagnitude (result), 1.0F) error 과 NaN

jh Seo·2023년 6월 4일
0

유니티

목록 보기
29/42

개요

Vector3.dot 함수를 이용해 두 사이의 각도를 구하려는데 뭔가 하고
로그 찍어보면서 분석해봤다.

if (collision.CompareTag("Player"))
{
    alertImg.SetActive(true);
    float dot = Vector3.Dot((transform.position-mainBody.transform.position).normalized, 
    (collision.transform.position - mainBody.transform.position).normalized);
    float angle = Mathf.Acos(dot) * Mathf.Rad2Deg;
    mainBody.transform.rotation = Quaternion.Euler(0, 0,angle);
}

이런식으로 오브젝트가 바라보는 벡터랑 오브젝트에서 플레이어로 향하는 벡터의
코사인 값을 Dot를 통해 구한 후,
아크코사인을 통해 라디안을 구하고 Rad2Deg값을 곱해줘서 각도를 구하는 방식으로 구현했다,

하지만 마지막 Quaternion.Euler값을 rotation에 넣어주는 부분에서

CompareApproximately (SqrMagnitude (result), 1.0F) error

이 오류가 계속 떠서 dot값과 angle값을 로그 찍어봤다.
그 결과 angle값에서 NaN이라고 떠서 뭔가 하고 검색해봤다.

NaN은 Not a Number의 줄임말로 숫자로 표현할 수 없는 값을 저렇게 표시한다.
대표적인 예로는 0으로 나눈 값이 있다.

따라서 dot값을 Acos식을 돌렸을 때 문제가 생겼구나 해서 다시보니
Dot함수의 첫번째 인자를 잘못 적었었다..(위의 코드는 수정 후이다)

profile
코딩 창고!

0개의 댓글