Repository navigation
Expand file tree
/
Copy pathPlayerControllerX.cs
More file actions
84 lines (67 loc) · 2.18 KB
/
Copy pathPlayerControllerX.cs
File metadata and controls
84 lines (67 loc) · 2.18 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControllerX : MonoBehaviour
{
public bool gameOver;
public float floatForce;
private float gravityModifier = 1.5f;
private Rigidbody playerRb;
public ParticleSystem explosionParticle;
public ParticleSystem fireworksParticle;
private AudioSource playerAudio;
public AudioClip moneySound;
public AudioClip explodeSound;
private AudioClip bounceSound;
public bool floatBoundry;
// Start is called before the first frame update
void Start()
{
Physics.gravity *= gravityModifier;
playerAudio = GetComponent<AudioSource>();
playerRb = GetComponent<Rigidbody>();
// Apply a small upward force at the start of the game
playerRb.AddForce(Vector3.up * 5, ForceMode.Impulse);
}
// Update is called once per frame
void Update()
{
// While space is pressed and player is low enough, float up
if (Input.GetKey(KeyCode.Space) && floatBoundry && !gameOver)
{
playerRb.AddForce(Vector3.up * floatForce);
}
if (transform.position.y > 14)
{
floatBoundry = false;
}
else
{
floatBoundry = true;
}
}
private void OnCollisionEnter(Collision other)
{
// if player collides with bomb, explode and set gameOver to true
if (other.gameObject.CompareTag("Bomb"))
{
explosionParticle.Play();
playerAudio.PlayOneShot(explodeSound, 1.0f);
gameOver = true;
Debug.Log("Game Over!");
Destroy(other.gameObject);
}
// if player collides with money, fireworks
else if (other.gameObject.CompareTag("Money"))
{
fireworksParticle.Play();
playerAudio.PlayOneShot(moneySound, 1.0f);
Destroy(other.gameObject);
}
else if (other.gameObject.CompareTag("Ground") && !gameOver)
{
playerRb.AddForce(Vector3.up * 10, ForceMode.Impulse);
playerAudio.PlayOneShot(bounceSound, 1.0f);
}
}
}