-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLerpTransform.cs
111 lines (100 loc) · 3.29 KB
/
LerpTransform.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
109
110
111
using UnityEngine;
// Esta clase permite mover o escalar un objeto desde sus valores iniciales hasta los valores finales (especificados en el inspector) con una duración
public class LerpTransform : AnimationScript
{
[Header("Settings")]
public float duration;
public bool loop;
public float pauseDuration;
private float pauseTime;
private bool pause;
[Header("Position")]
public bool setPosition;
public bool local;
public Vector3 finalPosition;
public bool useFinalTransform;
public Transform finalTransform;
Vector3 startPosition;
[Header("Rotation")]
public bool setRotation;
public bool localRot;
public Quaternion finalRotation;
Quaternion startRotation;
[Header("Scale")]
public bool setScale;
public Vector3 finalScale;
Vector3 startScale;
float startTime;
float t;
private void OnEnable()
{
pause = false;
startTime = Time.time;
if (local) startPosition = theAnimatedObject.localPosition;
else startPosition = theAnimatedObject.position;
startRotation = theAnimatedObject.rotation;
startScale = theAnimatedObject.localScale;
}
// Update is called once per frame
void Update()
{
t = (Time.time - startTime) / duration;
if (duration == 0) t = 1;
if (setPosition)
{
if (!useFinalTransform)
{
if(local) theAnimatedObject.localPosition = Vector3.Lerp(startPosition, finalPosition, t);
else theAnimatedObject.position = Vector3.Lerp(startPosition, finalPosition, t);
}
else
{
theAnimatedObject.position = Vector3.Lerp(startPosition, finalTransform.position, t);
}
}
if (setRotation)
{
if(finalRotation.x == 0 && finalRotation.y == 0 && finalRotation.z == 0)
{
theAnimatedObject.rotation = Quaternion.identity;
}
else if(localRot)
theAnimatedObject.localRotation = Quaternion.Lerp(startRotation, finalRotation, t);
else
theAnimatedObject.rotation = Quaternion.Lerp(startRotation, finalRotation, t);
}
if (setScale)
{
theAnimatedObject.localScale = Vector3.Lerp(startScale, finalScale, t);
}
if(t >= 1)
{
if (loop)
{
if (!pause)
{
pause = true;
pauseTime = Time.time;
}
if (local) theAnimatedObject.localPosition = startPosition;
else theAnimatedObject.position = startPosition;
theAnimatedObject.rotation = startRotation;
theAnimatedObject.localScale = startScale;
if (Time.time > pauseTime + pauseDuration)
{
pause = false;
t = 0;
startTime = Time.time;
}
}
else
{
this.enabled = false;
}
}
}
public override float GetDuration()
{
return duration;
}
}