summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/RootMotion/RootMotionData.cs
blob: 84a42ec18459f3486fd1190bd39d61b7103a13a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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;
    }

}