[Unity] Animation Blending

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

부드럽게 애니메이션 변환하기

Animator 탭에서 마우스 우클릭 후
Create State -> From New Blend Tree 를 선택한다.

그렇게 만들어진 Tree를 한번 더 더블클릭!

해당 창에서 우클릭 후 섞을 모션을 추가해주거나 Inspector 창을 이용해서 추가해 준다.

이제 변수를 추가해준다.
wait_run_ratio란 이름의 float 변수를 추가한 뒤

Inspector 창에서 Parameter를 변경하고 Threshold값을 0과 1로 맞춰준다.

wait_run_ratio의 값에 따라
0에 가까울수록 멈추는 동작으로
1에 가까울 수록 뛰는 동작으로 변하게 된다.

이제 코드 상에서 Mathf.Lerp 함수를 이용해 이 Blend 변수의 값을 서서히 변화시키도록 한다.

 float wait_run_ratio = 0;

    void Update()
    {
        if (_moveToDest)
        {
            wait_run_ratio = Mathf.Lerp(wait_run_ratio, 1, 10.0f * Time.deltaTime);
            Animator anim = GetComponent<Animator>();
            anim.SetFloat("wait_run_ratio", wait_run_ratio);
            anim.Play("WAIT_RUN");
        }
        else
        {
            wait_run_ratio = Mathf.Lerp(wait_run_ratio, 0, 10.0f * Time.deltaTime);
            Animator anim = GetComponent<Animator>();
            anim.SetFloat("wait_run_ratio", wait_run_ratio);
            anim.Play("WAIT_RUN");
        }

    }

Blend 적용 전

Blend 적용 후

profile
재밌는 코딩 공부

1개의 댓글

comment-user-thumbnail
2023년 7월 18일

좋은 글 감사합니다!

답글 달기