공백 내 한 포인트의 값을 다른 포인트의 값에서 빼면 그 결과는 한 오브젝트에서 다른 오브젝트를 “가리키는” 벡터 입니다.
// Gets a vector that points from the player's position to the target's.
var heading = target.position - player.position;
이 벡터는 타겟 오브젝트의 방향을 가리키고, 이 벡터의 크기는 두 포지션 사이의 거리와 같습니다. (예를 들어 발사체를 유도하기 위해)타겟의 방향과 타겟까지의 거리도 제시하는 정규화 된 벡터가 필요한 경우는 흔합니다. 오브젝트 사이의 거리는 지향 벡터의 크기와 같고, 이 벡터를 그 크기로 나눠서 노멀라이즈할 수 있습니다.
var distance = heading.magnitude;
var direction = heading / distance; // This is now the normalized direction.
방법은 크기와 정규화 된 프로퍼티를 모두 별도로 사용하는 것보다 바람직합니다. 둘 다 CPU를 많이 소모하기 때문입니다(모두 제곱근 연산을 수반함).
거리를 비교 용도로만 사용해야 하는 경우(예: 근접 검사) 크기 계산 자체를 피할 수 있습니다. sqrMagnitude 프로퍼티는 크기 값을 제곱을 제공하고 크기처럼 계산되지만 시간 소모적인 제곱근 연산이 불필요합니다. 크기를 알려진 거리에 비교하지 않고 크기의 제곱을 거리의 제곱에 비교할 수 있습니다.
if (heading.sqrMagnitude < maxRange * maxRange) {
// Target is within range.
}
그러면 실제 크기를 비교에 사용하는 것보다 훨씬 더 효율적입니다.
때로는 타겟을 향하는 지상 방향이 필요할 수 있습니다. 예를 들어 땅 위에 서있는 플레이어가 공중에 떠있는 타겟에 접근해야 하는 경우를 상상해 보아야 합니다. 플레이어의 포지션을 타겟의 포지션에서 빼면 결과 벡터가 타겟 방향으로 위쪽을 향합니다. 이 벡터는 플레이어의 트랜스폼 방향을 지정하는 데 적합하지 않습니다. 이 역시 위쪽을 향하기 때문입니다. 실제로 필요한 것은 플레이어의 포지션에서 타겟 바로 밑에 있는 지상 포지션까지의 벡터입니다. 이 벡터는 뺄셈 결과를 사용하고 Y 좌표를 0으로 설정하여 쉽게 얻을 수 있습니다.
var heading = target.position - player.position;
heading.y = 0; // This is the overground heading.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.