-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fly.js
95 lines (91 loc) · 2.11 KB
/
Fly.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var maxSpeed = 50.0;
var accelKey = "w";
var leftKey = "left";
var rightKey = "right";
var upKey = "up";
var downKey = "down";
var accel = 1.2;
var decel = 0.8;
var upDownSpeed = 20.0;
var turnSpeed = 20.0;
var speedToFly = 40.0;
var cam : GameObject;
var groundDetector : GameObject;
var explosion : GameObject;
private var curSpeed = 0.0;
private var curFall = 0.0;
private var isFalling = false;
static var isGrounded = true;
function Start () {
}
function Update () {
if(isFalling == false){
if(Input.GetKeyDown(accelKey)){
curSpeed ++;
}
if(Input.GetKey(accelKey)){
curSpeed += accel * Time.deltaTime;
}
}
else
{
curSpeed --;
}
if(Input.GetKey(accelKey)){
}
else{
if(isGrounded == false){
curSpeed -= decel * Time.deltaTime;
}
if(isGrounded == true){
curSpeed -= (decel * 2) * Time.deltaTime;
}
}
if(curSpeed > maxSpeed){
curSpeed = maxSpeed;
}
if(curSpeed < 0){
curSpeed = 0;
}
if(isGrounded == true){
if(curSpeed < speedToFly){
transform.rotation.x = 0;
transform.rotation.z = 0;
}
}
if(isGrounded == false){
if(curSpeed < speedToFly){
curFall += 9.8 * Time.deltaTime;
transform.Translate(Vector3.down * curFall * Time.deltaTime);
if(transform.position.y > 50){
groundDetector.SetActive(false);
isFalling = true;
}
}
}
transform.Translate(Vector3.forward * curSpeed * Time.deltaTime);
if(Input.GetKey(upKey)){
if(curSpeed >= speedToFly){
transform.Rotate(Vector3.right * (-1 * upDownSpeed) * Time.deltaTime);
}
}
if(Input.GetKey(leftKey)){
transform.Rotate(Vector3.up * (-1 * turnSpeed) * Time.deltaTime);
}
if(Input.GetKey(rightKey)){
transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime);
}
if(Input.GetKey(downKey)){
transform.Rotate(Vector3.right * turnSpeed * Time.deltaTime);
}
print(curSpeed);
}
function OnTriggerEnter(col : Collider){
if(col.tag == "Terrain"){
var cam1 : GameObject = Instantiate(cam, cam.transform.position, cam.transform.rotation);
cam1.SetActive(true);
var explo : GameObject = Instantiate(explosion, transform.position, transform.rotation);
Destroy(groundDetector);
Destroy(gameObject);
}
}