[Unity] Rotation

Yerin·2023년 7월 10일
0
post-thumbnail

여러가지 회전 방식

// 절대 회전값
_yAngle += Time.deltaTime * _speed;
transform.eulerAngles = new Vector3(0.0f, _yAngle, 0.0f);
//+- delta 변화하는 값을 이용
transform.Rotate(new Vector3(0.0f, Time.deltaTime * 100.0f, 0.0f));

transform.rotation = Quaternion.Euler(new Vector3(0.0f, _yAngle, 0.0f));

wasd를 누르면 캐릭터가 해당 방향을 바라보도록 하기

 		if (Input.GetKey(KeyCode.W))
        {
            transform.rotation = Quaternion.LookRotation(Vector3.forward);
        }

        if (Input.GetKey(KeyCode.S)) {
            transform.rotation = Quaternion.LookRotation(Vector3.back);
        }

        if (Input.GetKey(KeyCode.A)) {
			transform.rotation = Quaternion.LookRotation(Vector3.left);
        }

        if (Input.GetKey(KeyCode.D)) {
            transform.rotation = Quaternion.LookRotation(Vector3.right);
        }

❗ 조금 더 부드럽게 이동했으면 좋겠다! ❗

부드럽게 캐릭터 방향 전환하기

if (Input.GetKey(KeyCode.W))
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
        }

        if (Input.GetKey(KeyCode.S)) {
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
        }

        if (Input.GetKey(KeyCode.A)) {
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
        }

        if (Input.GetKey(KeyCode.D)) {
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
        }

Quaternion.Slerp(시작점, 끝점, 어느 점에 더 많은 비율로 갈것인지!)

ex)
Quaternion.Slerp(시작점, 끝점, 0.0f) : 움직이지 않는다.
Quaternion.Slerp(시작점, 끝점, 1.0f) : Quaternion.LookRotation()과 같게 된다.

이동 코드와 합치기

 if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * Time.deltaTime * _speed);
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
        }

        if (Input.GetKey(KeyCode.S)) {
            transform.Translate(Vector3.forward * Time.deltaTime * _speed);
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
        }

        if (Input.GetKey(KeyCode.A)) {
            transform.Translate(Vector3.forward * Time.deltaTime * _speed);
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
        }

        if (Input.GetKey(KeyCode.D)) {
            transform.Translate(Vector3.forward * Time.deltaTime * _speed);
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
        }

❗❗ 하지만 위 코드는 커브를 돌면서 돌아가는 경우가 발생한다! ❗❗

절대좌표를 이용하여 직관적인 방향으로 이동하도록 하자.

위와 달리 바라보고 있는 방향과 연관이 없으니 forward를 다시 방향에 맞게 수정해줘야한다.

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;
        }

profile
재밌는 코딩 공부

0개의 댓글