-
Notifications
You must be signed in to change notification settings - Fork 0
/
jetpack script.cs
70 lines (62 loc) · 1.37 KB
/
jetpack script.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
public class Jetpack : MonoBehaviour
{
public float thrust = 10f;
public float fuel = 100f;
public float maxFuel = 100f;
public float fuelConsumptionRate = 10f;
public ParticleSystem jetpackParticles;
public AudioSource jetpackAudio;
private bool isJetpacking = false;
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (Input.GetKey(KeyCode.Space) && fuel > 0)
{
StartJetpacking();
}
else
{
StopJetpacking();
}
}
private void FixedUpdate()
{
if (isJetpacking)
{
ConsumeFuel();
ApplyThrust();
}
}
private void ConsumeFuel()
{
fuel -= fuelConsumptionRate * Time.fixedDeltaTime;
if (fuel < 0)
{
fuel = 0;
StopJetpacking();
}
}
private void ApplyThrust()
{
rb.AddForce(Vector2.up * thrust, ForceMode2D.Force);
if (!jetpackAudio.isPlaying)
{
jetpackAudio.Play();
jetpackParticles.Play();
}
}
private void StartJetpacking()
{
isJetpacking = true;
}
private void StopJetpacking()
{
isJetpacking = false;
jetpackAudio.Stop();
jetpackParticles.Stop();
}
}