Unity는 Quaternion 클래스를 사용하여 게임 오브젝트의 3차원 방향을 저장하고, 이를 통해 한 방향에서 다른 방향으로의 상대 회전을 설명합니다.
이 페이지는 Quaternion 클래스의 개요, 그리고 이 클래스를 사용하는 스크립팅의 일반적인 용도에 대해 설명합니다. Quaternion 클래스의 모든 멤버에 대한 전체 레퍼런스는 Quaternion 스크립트 레퍼런스를 참조하십시오.
오일러 각(게임 오브젝트의 회전을 위해 인스펙터에 표시되는 X, Y, Z 값)과 Unity가 게임 오브젝트의 실제 회전을 저장하는 데 사용하는 기본 쿼터니언 값 간의 차이를 이해하고 있어야 합니다. 이 항목에 대한 기본 정보는 Unity의 회전 및 방향을 참조하십시오.
스크립트에서 회전 처리를 다루는 경우 Quaternion 클래스와 이 클래스의 함수를 사용하여 회전 값을 만들고 수정해야 합니다. 오일러 각을 사용할 수 있는 상황도 있지만, 다음 사항을 염두에 둬야 합니다. - 오일러 각을 처리하는 Quaternion 클래스 함수를 사용해야 합니다. - 회전의 오일러 값을 검색 및 수정하고 다시 적용하면 의도하지 않은 부작용이 발생할 수 있습니다(아래 참조).
Unity Quaternion 클래스의 여러 함수를 통해 오일러 각을 전혀 사용하지 않고도 회전을 만들고 조정할 수 있으며, 대부분의 일반적인 경우 이러한 함수들을 사용해야 합니다. 각각의 함수는 코드 샘플이 있는 스크립트 레퍼런스에 연결됩니다.
Transform 클래스도 쿼터니언 회전에 사용할 수 있는 다음의 메서드를 제공합니다.
일부의 경우 스크립트에서 오일러 각을 사용하는 것이 더 좋습니다. 이 경우 각을 변수로 유지하고 회전에 오일러 각으로 적용하는 데만 사용해야 하고, 궁극적으로 쿼터니언으로 저장되어야 합니다. 오일러 각을 쿼터니언에서 검색해서 가져올 수 있지만, 검색해서 가져온 후 수정하고 다시 적용하면 문제가 발생할 수 있습니다.
이러한 문제가 정확히 어떻게 발생하는지에 대한 자세한 내용은 eulerAngles 스크립트 레퍼런스 페이지를 참조하십시오.
아래에는 흔히 발생하는 잘못된 방법에 대한 몇 개의 예제가 있습니다. 설명을 위해 X축을 중심으로 게임 오브젝트를 초당 10도씩 회전시키려는 가상의 예제를 사용합니다. 다음은 잘못된 방법입니다.
// rotation scripting mistake #1
// the mistake here is that we are modifying the x value of a quaternion
// this value does not represent an angle, and does not produce desired results
void Update ()
{
var rot = transform.rotation;
rot.x += Time.deltaTime * 10;
transform.rotation = rot;
}
// rotation scripting mistake #2
// Read, modify, then write the Euler values from a Quaternion.
// Because these values are calculated from a Quaternion,
// each new rotation might return very different Euler angles, which might suffer from gimbal lock.
void Update ()
{
var angles = transform.rotation.eulerAngles;
angles.x += Time.deltaTime * 10;
transform.rotation = Quaternion.Euler(angles);
}
그리고 다음은 스크립트에서 오일러 각을 올바르게 사용하는 예입니다.
// Rotation scripting with Euler angles correctly.
// Store the Euler angle in a class variable, and only use it to
// apply it as an Euler angle, but never rely on reading the Euler back.
float x;
void Update ()
{
x += Time.deltaTime * 10;
transform.rotation = Quaternion.Euler(x,0,0);
}
자세한 내용은 Quaternion 스크립트 레퍼런스 페이지를 참조하십시오.
Quaternion
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.