-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController.cs
More file actions
108 lines (87 loc) · 3.29 KB
/
Copy pathPlayerController.cs
File metadata and controls
108 lines (87 loc) · 3.29 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//AudioSources
public AudioSource explosionSound;
//Booleans
public bool invincible = false;
public bool speedBoostActive = false;
//GameObjects
public GameObject playerShield;
public GameObject speedBoost;
//ParticleSystems
public ParticleSystem explosionPre;
//Scripts
private GameManager manager;
private SpawnManager spawn;
//Values
private int speedBoostDuration = 5;
private int speedMultipler = 3;
public float speed = 5.0f;
private void Awake()
{
//Import Scripts
manager = GameObject.Find("GameManager").GetComponent<GameManager>();
spawn = GameObject.Find("SpawnManager").GetComponent<SpawnManager>();
}
void Update()
{
//Player Movement
float vertical = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(vertical * -1, 0) * speed * Time.deltaTime;
transform.Translate(movement);
}
public void EnableSpeedBoost()
{
//Turn speed boost on
speedBoost.SetActive(true);
speedBoostActive = true;
speed *= speedMultipler;
StartCoroutine("DisableSpeedBoost");
for (int i = 0; i < spawn.spawnItems.Count; i++)
{
spawn.spawnItems[i].GetComponent<ObstacleMovement>().speed *= speedMultipler;
}
}
IEnumerator DisableSpeedBoost()
{
//Turn off speed boost after a given time
yield return new WaitForSeconds(speedBoostDuration);
speedBoostActive = false;
speedBoost.SetActive(false);
speed /= speedMultipler;
for (int i = 0; i < spawn.spawnItems.Count; i++)
{
spawn.spawnItems[i].GetComponent<ObstacleMovement>().speed /= speedMultipler;
}
}
void OnCollisionEnter2D(Collision2D other)
{
//Detect collision with game ending obstacles
if (other.gameObject.tag == "enemy" || other.gameObject.tag == "asteroid" || other.gameObject.tag == "laser")
{
if (invincible)
{
//If player is is invincible (has active shield) destroy obstacle and deactivate shield
ParticleSystem explosion = Instantiate(explosionPre, other.gameObject.transform.position, Quaternion.identity);
explosion.Play(); //Particle effect
explosionSound.Play(); //Audio
Destroy(other.gameObject);
invincible = false; //Turn off invincibility
playerShield.SetActive(false); //Turn off shield
spawn.spawnItems.Remove(other.gameObject); //Remove destroyed object from spawn list
}
else
{
//If player is not invincible
manager.targetted = false;
manager.gameOver = true; //Game over state
ParticleSystem explosion = Instantiate(explosionPre, gameObject.transform.position, Quaternion.identity);
explosion.Play(); //Particle effect
gameObject.SetActive(false); //Player disappears
}
}
}
}