[Unity] 회전 - Quaternion, Transform, Rotation

ChangJin·2024년 2월 12일
0

Unity

목록 보기
13/17
post-thumbnail

https://docs.unity3d.com/ScriptReference/Transform.Rotate.html

https://docs.unity3d.com/2018.1/Documentation/ScriptReference/Transform.Rotate.html

https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html


오일러각(Euler angle)

  • 3차원 공간에서 Object가 놓인 방향을 표시하기 위한 방법.
    • x축을 roll
    • y축을 pitch
    • z축을 yaw
  • 이런 오일러각을 이용해서 물체를 회전하면 두 축이 한 축으로 합쳐지는 경우가 생깁니다. 이런 현상을 짐벌락(Gimbal Lock) 이라고 합니다.



짐벌락(Gimbal Lock)

  • 두 축이 한 축으로 합쳐지기 때문에 제 역할을 할 수 없는 현상입니다



쿼터니언(Quaternion)

  • 유니티에선 짐벌락의 문제 때문에 쿼터니언을 사용합니다.
  • 사원수, 즉 복소수를 확장해서 만든 개념입니다
  • 쿼터니언은 여러가지 사용법이 있습니다.
    • transform.Rotate : 유니티에서 Obejct를 회전하게 해주는 함수.
      • Y축을 기준으로 회전하는 예시 3가지.
        transform.Rotate(Vector3.up *Time.deltaTime);
        transform.Rotate(0, Time.deltaTime, 0);
        transform.Rotate(Vector3.up, Time.deltaTime);

    • Transform.RotateAround
        ```csharp
        using UnityEngine;
        
        //Attach this script to aGameObject to rotate around the target position.
        public class Example :MonoBehaviour
        {
            //Assign aGameObject in the Inspector to rotate around
            publicGameObject target;
        
            voidUpdate()
            {
                // Spin the object around the target at 20 degrees/second.
                transform.RotateAround(target.transform.position,Vector3.up, 20 *Time.deltaTime);
            }
        }
        ```


  • 실제 사용 예시
    void Drag()
       {
           if (Input.GetMouseButton(1))
           {
               // while the mouse is held down the cube can be moved around its central axis to provide visual feedback
               mouseDelta = Input.mousePosition - previousMousePosition;
               mouseDelta *= 0.1f; // reduction of rotation speed
               transform.rotation = Quaternion.Euler(mouseDelta.y, -mouseDelta.x, 0) * transform.rotation;
           }
           else
           {
               // automatically move to the target position
               if (transform.rotation != target.transform.rotation)
               {
                   var step = speed * Time.deltaTime;
                   transform.rotation = Quaternion.RotateTowards(transform.rotation, target.transform.rotation, step);
               }
           }
           previousMousePosition = Input.mousePosition;
       }

profile
게임 프로그래머

0개의 댓글

관련 채용 정보