forked from tmokmss/com.unity.multiplayer.samples.coop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PositionLerper.cs
52 lines (44 loc) · 1.6 KB
/
PositionLerper.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
using UnityEngine;
namespace Unity.BossRoom.Utils
{
/// <summary>
/// Utility struct to linearly interpolate between two Vector3 values. Allows for flexible linear interpolations
/// where current and target change over time.
/// </summary>
public struct PositionLerper
{
// Calculated start for the most recent interpolation
Vector3 m_LerpStart;
// Calculated time elapsed for the most recent interpolation
float m_CurrentLerpTime;
// The duration of the interpolation, in seconds
float m_LerpTime;
public PositionLerper(Vector3 start, float lerpTime)
{
m_LerpStart = start;
m_CurrentLerpTime = 0f;
m_LerpTime = lerpTime;
}
/// <summary>
/// Linearly interpolate between two Vector3 values.
/// </summary>
/// <param name="current"> Start of the interpolation. </param>
/// <param name="target"> End of the interpolation. </param>
/// <returns> A Vector3 value between current and target. </returns>
public Vector3 LerpPosition(Vector3 current, Vector3 target)
{
if (current != target)
{
m_LerpStart = current;
m_CurrentLerpTime = 0f;
}
m_CurrentLerpTime += Time.deltaTime;
if (m_CurrentLerpTime > m_LerpTime)
{
m_CurrentLerpTime = m_LerpTime;
}
var lerpPercentage = m_CurrentLerpTime / m_LerpTime;
return Vector3.Lerp(m_LerpStart, target, lerpPercentage);
}
}
}