forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLerpFromOffset.cs
108 lines (100 loc) · 4.38 KB
/
LerpFromOffset.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LerpFromOffset : MonoBehaviour
{
/// <summary>
/// Effect Enum only to be used by this class, for chossing which interpolation effect to use
/// </summary>
private enum Effect
{
BOUNCE_OUT,
ELASTIC_OUT,
LINEAR,
CIRCULAR_OUT,
CIRCULAR_IN,
CIRCULAR_IN_OUT,
INVERSE_CIRCULAR_IN_OUT
}
[Header("Configuration")]
[Tooltip("The The Effect you would like to happen")]
[SerializeField] Effect m_effect = Effect.LINEAR;
[Tooltip("The direction you want the object to lerp from")]
[SerializeField] Vector3 m_offsetDirection = Vector3.zero;
[Tooltip("The the distnace you want to multiply the direction by")]
[SerializeField] float m_offsetDistance = 1.0f;
[Tooltip("How much time it will take for the object to reach its original position")]
[SerializeField] float m_timeToReachPosition = 1.5f;
[Tooltip("How much time it will take to start moving")]
[SerializeField] float m_delayStart = 1.5f;
//Memeber variables
private Vector3 m_offset = Vector3.zero;
private Vector3 m_desiredPos = Vector3.zero;
private float m_currentTime = 0.0f;
void OnEnable()
{
m_offset = Vector3.zero;
m_desiredPos = Vector3.zero;
m_currentTime = 0.0f;
Setup();
}
/// <summary>
/// Method that sets up all of the needed things and places the object at the calcuated offset
/// </summary>
void Setup()
{
//calculate offset position
m_offset = m_offsetDirection.normalized * m_offsetDistance;
//set the desired position to the current position
m_desiredPos = transform.position;
//move the object to the offset position to begin lerping
transform.position = m_desiredPos + m_offset;
}
void Update()
{
if (m_delayStart <= 0.0f)
{
//add deltaTime to the current time
m_currentTime += Time.deltaTime;
if (m_currentTime < m_timeToReachPosition)
{
//based on which effect is selected this will lerp from the curretn position to the desired position
switch (m_effect)
{
//Lerps using the BouceOut Interpolation
case Effect.BOUNCE_OUT:
transform.position = Vector3.LerpUnclamped(m_desiredPos + m_offset, m_desiredPos, Interpolation.BounceOut(m_currentTime / m_timeToReachPosition));
break;
//Lerps using the ElasticOut Interpolation
case Effect.ELASTIC_OUT:
transform.position = Vector3.LerpUnclamped(m_desiredPos + m_offset, m_desiredPos, Interpolation.ElasticOut(m_currentTime / m_timeToReachPosition));
break;
//Lerps using the Linear Interpolation
case Effect.LINEAR:
transform.position = Vector3.LerpUnclamped(m_desiredPos + m_offset, m_desiredPos, Interpolation.Linear(m_currentTime / m_timeToReachPosition));
break;
//Lerps using the CircularOut Interpolation
case Effect.CIRCULAR_OUT:
transform.position = Vector3.LerpUnclamped(m_desiredPos + m_offset, m_desiredPos, Interpolation.CircularOut(m_currentTime / m_timeToReachPosition));
break;
//Lerps using the CircularInOut Interpolation
case Effect.CIRCULAR_IN_OUT:
transform.position = Vector3.LerpUnclamped(m_desiredPos + m_offset, m_desiredPos, Interpolation.CircularInOut(m_currentTime / m_timeToReachPosition));
break;
//Lerps using the CircularIn Interpolation
case Effect.CIRCULAR_IN:
transform.position = Vector3.LerpUnclamped(m_desiredPos + m_offset, m_desiredPos, Interpolation.SineIn(m_currentTime / m_timeToReachPosition));
break;
}
return;
}
//set the current position to the desired position
transform.position = m_desiredPos;
}
else
{
//remove Deltatime from the m_delayStart
m_delayStart -= Time.deltaTime;
}
}
}