본문 바로가기
개발/C#

유니티 C# 물체 회전시키기 Quaternion 종류 및 간단 사용법

by SPNK 2023. 3. 31.
반응형

Quaternion.Euler

z축을 중심으로 z도, x축을 중심으로 x도, y축을 중심으로 y도 회전하는 회전을 반환합니다(순서대로 적용).

 

  • 코드 작성
Vector3 eulerAngles = new Vector3(45f, 90f, 0f);
Quaternion rotation = Quaternion.Euler(eulerAngles);

 

Quaternion.AngleAxis

Angle를 기준으로 각도를 회전하는 회전을 만듭니다.

 

  • 코드 작성
Vector3 axis = Vector3.up;
float angle = 45f;
Quaternion rotation = Quaternion.AngleAxis(angle, axis);

 

Quaternion.LookRotation

지정된 앞쪽 및 위쪽 방향으로 회전을 생성합니다.

 

Z축은 정방향에 정렬되고, X축은 정방향과 위쪽의 교차 곱에 정렬되며, Y축은 Z와 X의 교차 곱에 정렬됩니다.

 

  • 코드 작성
Vector3 targetPosition = new Vector3(5f, 0f, 0f);
Vector3 upVector = Vector3.up;
Quaternion rotation = Quaternion.LookRotation(targetPosition, upVector);

 

Quaternion.FromToRotation

방향에서 방향으로 회전하는 회전을 만듭니다.

일반적으로 변환 축 중 하나(예: y 축)가 월드 스페이스의 방향으로 목표 방향을 따르도록 변환을 회전하는 데 사용합니다.

 

  • 코드 작성
Vector3 fromDirection = Vector3.forward;
Vector3 toDirection = Vector3.right;
Quaternion rotation = Quaternion.FromToRotation(fromDirection, toDirection);

 

Quaternion.eulerAngles

 

회전의 오일러 각도 표현을 반환하거나 설정합니다.

오일러 각은 개별 축을 중심으로 세 개의 개별 회전을 수행하여 3차원 회전을 나타낼 수 있습니다. Unity에서는 이러한 회전이 Z축, X축, Y축을 중심으로 순서대로 수행됩니다.

이 프로퍼티를 설정하여 쿼터니언의 회전을 설정할 수 있으며, 이 프로퍼티를 읽어 오일러 각도 값을 읽을 수 있습니다.

 

  • 코드 작성
Quaternion rotation = Quaternion.identity;
Vector3 eulerAngles = rotation.eulerAngles;

 

Quaternion.Lerp

 

a와 b 사이를 t로 보간한 후 결과를 정규화합니다. 매개변수 t는 [0, 1] 범위로 고정됩니다.

Slerp보다 빠르지만 회전이 멀리 떨어져 있으면 더 나빠 보입니다.

 

  • 코드 작성
Quaternion startRotation = Quaternion.identity;
Quaternion endRotation = Quaternion.Euler(0f, 90f, 0f);
float t = 0.5f;
Quaternion interpolatedRotation = Quaternion.Lerp(startRotation, endRotation, t);

 
 

반응형

댓글