summaryrefslogtreecommitdiff
path: root/Assets/Tools/RootMotion/RootMotionData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Assets/Tools/RootMotion/RootMotionData.cs')
-rw-r--r--Assets/Tools/RootMotion/RootMotionData.cs54
1 files changed, 54 insertions, 0 deletions
diff --git a/Assets/Tools/RootMotion/RootMotionData.cs b/Assets/Tools/RootMotion/RootMotionData.cs
new file mode 100644
index 00000000..f60689ad
--- /dev/null
+++ b/Assets/Tools/RootMotion/RootMotionData.cs
@@ -0,0 +1,54 @@
+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<Vector3> 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);
+ }
+
+ /// <summary>
+ /// 返回移动量,加到当前position上
+ /// </summary>
+ /// <param name="prevTime">上一次计算root motion的单位时间</param>
+ /// <param name="curTime">本次取root motion的时间</param>
+ /// <returns></returns>
+ public Vector3 GetRootMotionDistance(float prevTime, float curTime)
+ {
+ Vector3 p1 = GetRootMotion(prevTime);
+ Vector3 p2 = GetRootMotion(curTime);
+ return p2 - p1;
+ }
+
+}
+
+public static class RootMotionUtility
+{
+ // zy平面的移动改为xy平面的移动
+ public static Vector3 ExchangeXZ(Vector3 dest)
+ {
+ float z = dest.z;
+ dest.z = dest.x;
+ dest.x = z;
+ return dest;
+ }
+}