-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathVector3Animation.cs
More file actions
43 lines (36 loc) · 826 Bytes
/
Vector3Animation.cs
File metadata and controls
43 lines (36 loc) · 826 Bytes
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;
using UnityEngine;
namespace CameraTools
{
public class Vector3Animation
{
Vector3[] positions;
float[] times;
public Vector3Animation(Vector3[] pos, float[] times)
{
this.positions = pos;
this.times = times;
}
public Vector3 Evaluate(float t)
{
int startIndex = 0;
for(int i = 0; i < times.Length; i++)
{
if(t >= times[i])
{
startIndex = i;
}
else
{
break;
}
}
int nextIndex = Mathf.RoundToInt(Mathf.Min(startIndex + 1, times.Length - 1));
float overTime = t - times[startIndex];
float intervalTime = times[nextIndex] - times[startIndex];
if(intervalTime <= 0) return positions[nextIndex];
float normTime = overTime/intervalTime;
return Vector3.Lerp(positions[startIndex], positions[nextIndex], normTime);
}
}
}