blob: f60689ad20c56fe2f6a4414b26145a5ccce0605d (
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
43
44
45
46
47
48
49
50
51
52
53
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;
}
}
|