전체 코드

1. Transform과 Rotation의 관계

  • Transform 컴포넌트는 Unity에서 오브젝트의 위치(Position), 회전(Rotation), 크기(Scale)를 관리한다.
  • Rotation(회전)transform.rotation 속성을 통해 관리되며, 이 값은 Quaternion 타입이다.
  • Quaternionx, y, z, w의 4차원 값을 사용하여 짐벌 락(Gimbal Lock) 문제를 방지하고 회전을 표현한다.
  • 하지만 사용자가 X, Y, Z 축의 회전값을 직관적으로 다루기 위해서는 eulerAngles를 사용할 수 있다.

2. 절대 회전값 설정 (Euler Angles)

transform.eulerAngles

transform.eulerAngles = new Vector3(0.0f, _yAngle, 0.0f);
  • eulerAnglesVector3 값을 사용해 X, Y, Z 축 회전 값을 직접 설정할 수 있다.
  • 절대적인 회전값을 설정하는 데 사용된다.

🚨 주의 사항

transform.eulerAngles += new Vector3(0.0f, _yAngle, 0.0f);  // ❌ 오류 가능성
  • eulerAngles360도를 넘어가면 정확한 계산이 보장되지 않음.
  • 덧셈, 뺄셈을 통한 상대적인 회전값 적용은 추천되지 않는다.

3. 상대 회전값 설정 (Rotate 함수)

transform.Rotate

transform.Rotate(new Vector3(0.0f, Time.deltaTime * 100.0f, 0.0f));
  • Rotate(Vector3)를 사용하면 현재 회전값에서 특정 각도만큼 더 회전할 수 있다.
  • 프레임별 회전 시 유용하게 사용된다.

4. Quaternion을 활용한 회전

1. transform.rotation

  • rotation은 Transform의 회전값을 Quaternion 형태로 저장하는 속성이다.

2. Quaternion.Euler

transform.rotation = Quaternion.Euler(new Vector3(0.0f, _yAngle, 0.0f));
  • Euler(Vector3)를 사용하여 오일러 각도를 Quaternion으로 변환할 수 있다.
  • 절대 회전값을 설정할 때 유용하다.

3. Quaternion.LookRotation

transform.rotation = Quaternion.LookRotation(Vector3.forward);
  • 특정 방향을 바라보는 회전값을 설정할 때 사용된다.
  • 적이 플레이어를 바라보게 하거나, 캐릭터가 이동 방향을 바라보도록 설정하는 데 사용된다.

4. Quaternion.Slerp

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
  • 두 개의 회전값을 부드럽게 보간하는 함수.
  • Slerp(A, B, t): A에서 B까지 t 비율만큼 회전.

5. 예제 코드 분석

1. 절대 회전값 설정

_yAngle += Time.deltaTime * 100.0f;
transform.eulerAngles = new Vector3(0.0f, _yAngle, 0.0f);
  • _yAngle 값을 매 프레임 증가시켜 Y축 회전을 지속적으로 증가.

2. 상대 회전값 설정

transform.Rotate(new Vector3(0.0f, Time.deltaTime * 100.0f, 0.0f));
  • Rotate()를 사용하여 현재 회전값에서 상대적으로 회전.

3. Quaternion을 통한 절대 회전

transform.rotation = Quaternion.Euler(new Vector3(0.0f, _yAngle, 0.0f));
  • Quaternion.Euler를 통해 회전을 설정.

4. 특정 방향 바라보기

transform.rotation = Quaternion.LookRotation(Vector3.forward);
  • 오브젝트가 Z축 양의 방향(Vector3.forward) 을 바라보도록 설정.

5. 부드러운 회전 (Slerp)

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
  • 현재 회전값에서 목표 회전값으로 부드럽게 회전.

6. WASD 이동 + 회전 코드 분석

void Update()
{
    if (Input.GetKey(KeyCode.W)) // 전진
    {
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
        transform.position += Vector3.forward * Time.deltaTime * _speed;
    }
    if (Input.GetKey(KeyCode.S)) // 후진
    {
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
        transform.position += Vector3.back * Time.deltaTime * _speed;
    }
    if (Input.GetKey(KeyCode.A)) // 좌측
    {
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
        transform.position += Vector3.left * Time.deltaTime * _speed;
    }
    if (Input.GetKey(KeyCode.D)) // 우측
    {
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
        transform.position += Vector3.right * Time.deltaTime * _speed;
    }
}

분석

  1. 키 입력 감지

    • Input.GetKey(KeyCode.W): 키 입력을 감지하여 이동.
  2. 부드러운 회전

    • Quaternion.Slerp(transform.rotation, 목표 회전값, 0.2f)을 사용해 회전값을 부드럽게 변경.
  3. 이동 처리

    • transform.position += Vector3.forward * Time.deltaTime * _speed;
    • position을 변경하여 이동.

profile
李家네_공부방

0개의 댓글