-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelicopter.js
More file actions
160 lines (139 loc) · 3.78 KB
/
Helicopter.js
File metadata and controls
160 lines (139 loc) · 3.78 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var vRotor : GameObject;
var vRotorBack : GameObject;
var vRotorRPM : int = 5000;
var vPitch : float = 0;
var vAltitudelock : boolean = false;
var vMaxSpeed : int = 140;
private var newVelocity : Vector3;
private var vLockAlt : float;
function FixedUpdate ()
{
RotorRotation();
Controls();
Lift();
}
// This function rotates the blades. Assign vRotor and vRotorBack and modify vRotorRPM to make it spin up when the engine is started etc.
function RotorRotation ()
{
if(vRotor != null)
{
vRotor.transform.Rotate(Vector3.up * Time.deltaTime * vRotorRPM, Space.Self);
}
if(vRotorBack != null)
{
vRotorBack.transform.Rotate(Vector3.right * Time.deltaTime * vRotorRPM, Space.Self);
}
}
//This function controls the input. Modify to make the helicopter behave differently or add AI.
function Controls ()
{
//This if-statement controls the pitch of the helicopter. Modify the 1000 to make it pitch slower.
if(Input.GetAxis("Vertical"))
{
if(Input.GetAxis("Vertical") > 0)
{
rigidbody.AddRelativeTorque(Vector3.right * 300);
}
if(Input.GetAxis("Vertical") < 0)
{
rigidbody.AddRelativeTorque(Vector3.left * 300);
}
}
//This if-statement controls the roll of the helicopter. Modify the 200 to make it pitch slower.
if(Input.GetAxis("Horizontal"))
{
if(Input.GetAxis("Horizontal") > 0)
{
rigidbody.AddRelativeTorque(Vector3.back * 100);
}
if(Input.GetAxis("Horizontal") < 0)
{
rigidbody.AddRelativeTorque(Vector3.forward * 100);
}
}
//The next two if-statements modify the yaw of the helicopter. Not that the input keys are not hardcoded, you need to assign "RotateRight" and "RotateLeft" in Edit->Project Settings->Input.
if(Input.GetButton("RotateRight"))
{
rigidbody.AddRelativeTorque(Vector3.up * 250);
}
if(Input.GetButton("RotateLeft"))
{
rigidbody.AddRelativeTorque(Vector3.down * 250);
}
//The next two if-statements modify the pitch that makes the helicopter rise or sink. Hovering altitude for a 1000kg rigidbody seems to be around 10 Pitch.
if(Input.GetButton("PitchUp"))
{
vPitch = vPitch + 0.1;
AltCheck();
}
//The negative pitch allows for uplift while flying upside down like so called 360° stunt helicopters.
if(Input.GetButton("PitchDown"))
{
vPitch = vPitch - 0.1;
AltCheck();
}
//This statement locks the altitude to provide a hovering mode and make the control easier.
if(Input.GetButtonUp("AltitudeToggle"))
{
if(vAltitudelock == false)
{
//rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
vLockAlt = transform.position.y;
vAltitudelock = true;
}
else
{
//rigidbody.constraints = RigidbodyConstraints.None;
vAltitudelock = false;
}
}
if(vAltitudelock == true)
{
if(transform.position.y > vLockAlt)
{
vPitch = vPitch - 0.05;
if(vPitch < 20)
{
vPitch = 20;
}
vLockAlt = transform.position.y;
}
if(transform.position.y < vLockAlt)
{
vPitch = vPitch + 0.05;
AltCheck();
vLockAlt = transform.position.y;
}
}
//This statement limits the velocity of the helicopter.
if(rigidbody.velocity.z >= vMaxSpeed * 0.05)
{
rigidbody.velocity.z = vMaxSpeed * 0.05;
}
}
//This function provides lift.
function Lift()
{
rigidbody.AddRelativeForce(Vector3.up * vPitch * 400);
}
//This function checks if the rotor pitch is within limits.
function AltCheck()
{
if(vPitch > 40)
{
vPitch = 40;
}
if(vPitch < -30)
{
vPitch = -30;
}
}
//This function displays the label. You can remove it completely.
function OnGUI()
{
GUI.Box(Rect(5,5,400,90),"");
GUI.Label (Rect (10, 10, 500, 20), "Pitch: W/S, Roll: A/D, Yaw: Q/E, Rotorpitch: T/G, Altitude Lock: F");
GUI.Label (Rect (10, 30, 500, 20), "Rotorpitch: "+vPitch);
GUI.Label (Rect (10, 50, 500, 20), "Altitude Lock: "+vAltitudelock);
GUI.Label (Rect (10, 70, 500, 20), "Speed: "+rigidbody.velocity.z * 20);
}