/* 각 축별 각을 이용해 회전을 하는 함수 */

 

Rotate( Vector3 오일러각, Space.Self ); 

 

Rotate( Vector3, float 각도, Space Self );

 

Rotate( float X각, float Y각, float Z각, Space.Self ); 

 

 

 

 

/* 특정 점을 기준으로 축을 잡아 회전하는 메소드 */

 

RotateAround( Vector3 원점, Vector3 축, flaot 각도 );

 

 

 

 

/* Quaternion */

 

Quaternion.Lookrotation( Vector3 방향 ); // 해당 방향으로 쿼터니언 생성

 

Quaternion.Angle( Quaternion rotation, Quaternion rotation ); // 두 쿼터니언간의 각도 (flaot)

 

Quaternion.Euler( Vector3 오일러각 ); // 오일러각에 해당하는 쿼터니언 생성

 

Quaternion.Slerp( Quaternion from, Quaternion to, float time ); // to에서 from으로 시간에 따른 변화값

 

Quaternion.FromToRotation( Vector3 from, Vector3 to ); // to에서 from으로의 쿼터니언

 

Quaternion.Identity( ); // 회전 없음

 

 

 

 

/* Quaternion 활용 */

 

1. 특정 벡터에서 쿼터니언 얻어오기

 

Quaternion rotValue = Quaternion.LookRotation( target.position );

 

rotValue를 rotation객체에 넣으면 자동으로 해당 방향으로 회전처리한다

 

 

 

 

2. 특정 x,y,z각으로 회전

 

transform.rotation = Quaternion.Euler( X, Y, Z );

 

해당 각으로 회전한다. 오일러 회전처럼 축이 변화하지 않으므로, 동일한 값은 Update루틴에 넣어도 동일한 회전으로 유지된다. (오일러 회전은 계속 변한다)

 

 

 

 

3. 특정 지점을 원점으로 회전

 

float distance = Vector3.Distance( A, B ); // 기준점과의 거리 저장

 

Quaternion quat = Quaternion.Euler( new Vector3( 30, 0, 0 ); // X축을 기준으로 30도 회전

 

transform.rotation = quat; // 해당 쿼터니언을 rotation으로 지정

 

transform.position = Vector3 (기준점);

 

transform.position -= quat * Vector3.forward * distance; // 포워드벡터를 quat으로 회전시키고, 해당 축을 기준으로 distance만큼 뒤로 이동

 

transform.LookAt( Vector3 (기준점) ); // 이동처리 후 기준점을 바라본다

 

 

 

 

 

/* 캐릭터를 따라다니는 스무스 카메라 */

 

이 소스는 rotation 변수에 넣어 변환 파이프라인을 타지않고, 직접 포지션을 변경

 

타겟을 따라다니는 카메라는 타겟을 원점으로 Y축을 잡는다

 

카메라는 해당 Y축을 주변으로 회전하면 된다

 

 

public Transform target; // 따라다닐 타겟

 

// 카메라의 고정값 설정

public float distance = 10.0f;

 

public float height = 5.0f;

 

// 변경시 변위

float heightDamping = 2.0f;

 

float rotationDamping = 3.0f;

 

 

// 모든 업데이트 이후 호출

void LateUpdate( )

{

    // 현재 카메라의 Y축 앵글과 높이

    float currentAngle = transform.eulerAngles.y;

    float currentHeight = transform.position.y;

 

    // 타겟의 앵글과 높이

    float targetAngleY = target.eulerAngles.y;

    float targetHeight = target.position.y + height;

 

    // 원하는 앵글을 시간변위에 따라 현재 앵글값 얻음

    currentAngleY = Mathf.LerpAngle( currentAngleY, targetAngleY, rotationDamping * Time.deltaTime );

 

    // 높이도 똑같이

    currentHeight = Mathf.Lerp( currentHeight, targetHeight, heightDamping * Time.deltaTime );

 

    // 이동할 앵글을 회전으로 변환

    Quaternion newRotation = Quaternion.Euler( 0, currentAngleY, 0 );

 

    // 카메라의 포지션 이동

    Vector3 newPosition = transform.position;

 

    newPosition.position = target.position;

 

    newPosition.position -= newRotation * Vector3.forward * distance;

 

    // 높이 설정

    newPosition.y = currentHeight;

 

    // 최종 이동

    transform.position = newPosition;

 

    // 타겟을 바라보게 설정

    transform.LookAt( target );

 

}


 [출처] Rotate/Quaternion|작성자 라파공

[출처] Rotate/Quaternion|작성자 라파공


+ Recent posts