using System.Collections; using System.Collections.Generic; using UnityEngine; [CreateAssetMenu(fileName = "RootMotion Data")] // 单个动画的root motion public class RootMotionData : ScriptableObject { public string animationName; public int frameCount; public List positionList; public float animationLength; public float fps; public Vector3 GetRootMotion(float normalTime) { normalTime = Mathf.Clamp(normalTime, 0, 1); int prevFrame = (int)Mathf.Floor((frameCount - 1) * normalTime) % frameCount; int nextFrame = (int)Mathf.Ceil((frameCount - 1) * normalTime) % frameCount; float frameRate = 1 / fps; float t = (normalTime * animationLength - prevFrame * frameRate) / frameRate; return Vector3.Lerp(positionList[prevFrame], positionList[nextFrame], t); } /// /// 返回移动量,加到当前position上 /// /// 上一次计算root motion的单位时间 /// 本次取root motion的时间 /// public Vector3 GetRootMotionDistance(float prevTime, float curTime) { Vector3 p1 = GetRootMotion(prevTime); Vector3 p2 = GetRootMotion(curTime); return p2 - p1; } }