-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lerp.cs
65 lines (53 loc) · 1.46 KB
/
Lerp.cs
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
55
56
57
58
59
60
61
62
63
64
65
namespace Utilities
{
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lerp<T> : IEnumerator<T>
{
private readonly T _end;
private readonly Func<T, T, float, T> _lerp;
private readonly T _start;
private readonly float _targetDuration;
private float _elapsedTime;
private bool _timeHasElapsed;
public Lerp(T start, T end, float targetDuration, Func<T, T, float, T> lerp)
{
_start = start;
_end = end;
_lerp = lerp;
_targetDuration = targetDuration;
Current = _start;
}
public void Dispose()
{
throw new NotImplementedException();
}
public bool MoveNext()
{
if (_timeHasElapsed)
{
return false;
}
if (_elapsedTime >= _targetDuration)
{
_timeHasElapsed = true;
}
Current = _lerp(_start, _end, _elapsedTime / _targetDuration);
_elapsedTime += Time.deltaTime;
return true;
}
public void Reset()
{
_elapsedTime = 0f;
Current = _start;
_timeHasElapsed = false;
}
public T Current { get; private set; }
object IEnumerator.Current
{
get { return Current; }
}
}
}