-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMover.js
48 lines (40 loc) · 1.07 KB
/
Mover.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
#pragma strict
//----------------------------------------
// General component for things that move
// Meant to be controlled by other components
//----------------------------------------
var speed:float;
private var goalPos:Vector3;
private var direction:Vector3;
enum MoverState { STILL, TARGET, DIRECTION };
private var state = MoverState.STILL;
function MoveTo( p:Vector3 ) : void
{
goalPos = p;
state = MoverState.TARGET;
}
function MoveInDirection( d:Vector3 ) : void
{
direction = d;
direction.Normalize();
state = MoverState.DIRECTION;
}
function GetState() : MoverState { return state; }
function Update () {
if( state == MoverState.TARGET ) {
var goalDist = Vector2.Distance( goalPos, transform.position );
var maxDist = speed * Time.deltaTime;
if( goalDist > maxDist ) {
var dir = goalPos - transform.position;
dir.Normalize();
transform.position += dir * maxDist;
}
else {
transform.position = goalPos;
state = MoverState.STILL;
}
}
else if( state == MoverState.DIRECTION ) {
transform.position += speed * Time.deltaTime * direction;
}
}