-
Notifications
You must be signed in to change notification settings - Fork 3
/
FlyAnim.js
78 lines (61 loc) · 1.62 KB
/
FlyAnim.js
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
// Quick component for making game objects do "fly" animations - ie. go from one
// spot to another in a non-linear way, useful for posting scores, etc.
var applyAsDelta = false;
var start : Vector3;
var end : Vector3;
var animSecs : float = 1.0;
var inScreenSpace = false;
var mainCamera : Camera = null;
var hideOnEnd = true; // disable rendering when animation is done?
// set this flag to show it before animation
// but next time the anim is triggered, it will be hidden again
private var showUntilNextTrigger = false;
private var remainSecs : float = 0.0;
private var posAtStart:Vector3;
function ShowButDonotAnim()
{
showUntilNextTrigger = true;
}
function Hide()
{
showUntilNextTrigger = false;
}
function StartAnim() {
showUntilNextTrigger = false; // reset this flag
remainSecs = animSecs;
if( applyAsDelta )
posAtStart = transform.position;
}
function Play() { StartAnim(); }
function Update () {
if( remainSecs > 0.0 )
{
var alpha = 1 - remainSecs/animSecs;
var value = Vector3.Lerp( start, end, alpha*alpha );
if( inScreenSpace ) {
var ray = mainCamera.ScreenPointToRay( value );
var t = -ray.origin.z / ray.direction.z;
value = ray.origin + t*ray.direction;
}
if( applyAsDelta )
transform.position = posAtStart + value;
else
transform.position = value;
if( GetComponent.<Renderer>() )
GetComponent.<Renderer>().enabled = true;
remainSecs -= Time.deltaTime;
}
else
{
if( GetComponent.<Renderer>() ) {
if( showUntilNextTrigger )
GetComponent.<Renderer>().enabled = true;
else {
if( hideOnEnd )
{
GetComponent.<Renderer>().enabled = false;
}
}
}
}
}