summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/RootMotion/RootMotionData.cs
blob: 4fe2db97db408501a38a6df5c34eb9f62f40c985 (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
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);
    }

}