Unity

RigidbodyConstraints : 회전과 위치 제어

코샵 2024. 3. 9. 10:17
반응형
유니티 RigidbodyConstraints는 리지드바디의 회전과 위치를 제어하는 데 사용되는 enum입니다. 각 옵션은 다음과 같은 의미를 가집니다.

 

회전 제어:

  • FreezeRotationX: X축 회전을 동결합니다. (X축 회전 불가능)
  • FreezeRotationY: Y축 회전을 동결합니다. (Y축 회전 불가능)
  • FreezeRotationZ: Z축 회전을 동결합니다. (Z축 회전 불가능)

위치 제어:

  • FreezePositionX: X축 이동을 동결합니다. (X축 이동 불가능)
  • FreezePositionY: Y축 이동을 동결합니다. (Y축 이동 불가능)
  • FreezePositionZ: Z축 이동을 동결합니다. (Z축 이동 불가능)

예시

단일 제어 

Rigidbody rigid = GetComponent<Rigidbody>();

// X축 회전 동결
rigid.constraints = RigidbodyConstraints.FreezeRotationX;

 

 

| 연산자를 사용해 여러개 제어

// X, Y축 회전 동결
rigid.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;

// X, Y, Z축 회전 동결
rigid.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;

// X축 회전, Y, Z축 이동 동결
rigid.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;

 

기존 제약 유지

// X축 회전 추가
rigidbody.constraints |= RigidbodyConstraints.FreezeRotationX;

// Y축 회전 제거
rigidbody.constraints &= ~RigidbodyConstraints.FreezeRotationY;

 

비트 마스크 활용

// X, Y축 회전 동결
int mask = (int)RigidbodyConstraints.FreezeRotationX | (int)RigidbodyConstraints.FreezeRotationY;
rigid.constraints = (RigidbodyConstraints)mask;

 

활용 방법

  • 캐릭터 회전 제한: 캐릭터가 특정 방향으로만 회전하도록 제어
  • 오브젝트 고정: 오브젝트가 특정 위치에서 움직이지 않도록 제어
  • 게임 플레이 제작: 움직임 제한을 이용한 퍼즐, 탈출 게임 등 제작

추가 정보