Pv_log

벡터를 사용해 게임 오브젝트 사이의 거리 계산하는 방법 3가지 본문

Develop Study/Unity

벡터를 사용해 게임 오브젝트 사이의 거리 계산하는 방법 3가지

Priv 2021. 3. 22. 22:19

 

 

 

 


출처

 

오브젝트간 거리 체크 / Vector3.Distance, magnitude, sqrMagnitude

유니티 버전 2017.2 오브젝트간의 transform position 거리를 체크하고 싶을 때, 세 가지 방법이 있다. 1) ...

blog.naver.com


 

1. Vector3.Distance

2번 항목에서 설명할 Vector3.magnitude와 성능과 사용 방법은 동일하다.

정확한 거리를 계산해 반환하지만, Vector3.sqrMagnitude보다는 속도가 느리다.

 


참고

 

Unity - Scripting API: Vector3.Distance

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com


 

 


 

2. Vector3.magnitude

1번 항목인 Vector3.Distance와 성능과 기능은 동일하다.

3축 사이의 정확한 거리를 계산해 Vector3 타입으로 반환한다.

 


출처

 

Unity - Scripting API: Vector3.magnitude

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com


 

 


 

3. Vector3.sqrMagnitude

1번, 2번 항목과 다르게 제곱 값을 루트 계산 없이 그대로 반환한다.

정확한 거리는 측정할 수 없으므로, 두 벡터 사이에 무엇이 더 크고 작은 지 판단하기 위한 용도로 사용하기에 적합하다.

 


참고

 

Unity - Scripting API: Vector3.sqrMagnitude

The magnitude of a vector v is calculated as Mathf.Sqrt(Vector3.Dot(v, v)). However, the Sqrt calculation is quite complicated and takes longer to execute than the normal arithmetic operations. Calculating the squared magnitude instead of using the magnitu

docs.unity3d.com


 

 


Comments