Game !

[Unity] 회전 정리2019. 1. 7. 16:26




Vector3.Lerp(from 벡터 , to 벡터 , 시간 t)

transform.position = Vector3.Lerp(start.position, end.position, Time.time); 보통 이런식으로 많이 사용. 

start지점에서 end지점으로 이동한다. 



Quaternion : 회전을 위한 함수 라고 간단하게 생각하자.



Quaternion.identity : 기화 시 사용.




Quaternion.LookRotation(벡터값) : 쉽게 얘기해서 target을 기준으로 회전한다. similar to the LookAt() method, the object is always watching objects.


public class LookAtScript : MonoBehaviour 

{

    public Transform target;

    

    void Update () 

    {

        Vector3 relativePos = target.position - transform.position;

        transform.rotation = Quaternion.LookRotation(relativePos);

    }

}




Quaternion.Euler(x,y,z): 각 축을 기준으로 기울이기.

public float tilt;//기울기

float HorizontalMove = Input.GetAxis("Horizontal");

HorizontalMove = HorizontalMove * Speed * Time.deltaTime;

GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 0.0f, HorizontalMove*-tilt);

위의 코드는 리지드바디의 회전(rotation)을 z축을 기준으로 하는 것.




Quaternion.FromToRotation(from벡터, to벡터,회전속도) :

from벡터가 to벡터 기준으로 확 기울어 버린다. 




Quaternion.Slerp(쿼터니언A, 쿼터니언B,t): A에서 B로 회전 회전속도는 t

위에서 말했던 LookRotation과 효과는 같다. 오브젝트가 회전하여 target을 바라본다.

그러면 왜 Slerp을 사용할까? 그것은 실제 유니티의 play버튼을 누르면 알 수 있다.

LookRotation이 play버튼을 누르자마자(다른 함수없이 LookRotation만 사용!!) target을 바라보는 반면,

Slerp은 서서히 타겟방향으로 회전한다. 즉, 서서히 몸을 돌리는 것 처럼 보인다.







Quaternion.Lerp(Quaternion from, Quaternion to, float t) vs Quaternion.Slerp(Quaternion from, Quaternion to, float t)


Lerp이 Slerp보다 빠르다. 대신 멀리 to에 들어가는 퀴테니언이 멀 경우 회전이 나쁘게 보이는 단점이 있다.

(This is faster than Slerp but looks worse if the rotations are far apart.)




출처 : 

http://supersoftware.tistory.com