애니메이션이 ‘고정’으로 제공되는 경우가 있는데, 이러한 애니메이션을 씬에 배치하면 캐릭터가 움직이지 않습니다. 이는 애니메이션에 ‘루트 모션’이 포함되어 있지 않은 것입니다. 이를 위해 스크립트에서 루트 모션을 수정할 수 있습니다. 모든 요소를 결합하려면 아래 단계를 따르십시오. 단, 동일한 결과를 얻을 수 있는 배리에이션은 다양하게 있으며, 다음은 하나의 방법일 뿐입니다.
마지막으로, 모션을 제어하려면 OnAnimatorMove 콜백을 구현하는 RootMotionScript.cs 스크립트를 생성해야 합니다.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class RootMotionScript : MonoBehaviour {
void OnAnimatorMove()
{
Animator animator = GetComponent<Animator>();
if (animator)
{
Vector3 newPosition = transform.position;
newPosition.z += animator.GetFloat("Runspeed") * Time.deltaTime;
transform.position = newPosition;
}
}
}
RootMotionScript.cs를 ‘Dude’ 오브젝트에 연결해야 합니다. 이렇게 하면 Animator 컴포넌트가 스크립트에 OnAnimatorMove 함수가 있는지 감지하고 Apply Root Motion 프로퍼티를 _Handled by Script_로 표시합니다.