Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Closeときどきアニメーションは“不動” となっているものがあり,つまりシーンにおいたときに,アニメーションつきキャラクターが動かないことがあります。言い換えると,アニメーションに “ルートモーション” がついていません。この場合,スクリプトからルートーモーションを修正することが出来ます。全てをつなぎあわせるには,次の手順にしたがって進めます(同じ結果を得られる方法は複数あって,これはあくまでそのひとつであることに留意して下さい)。
最後に,モーションを制御するためには, 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” にアタッチします。アニメータコンポーネントは OnAnimatorMove があるスクリプトを検知して,Apply Root Motion プロパティに Handled by Script と表示されます。