In the upgrade to the space shooter tutorial I am working on I have added a weapon upgrade. This currently gives the player 3 levels of firepower. Collecting the upgrade works great but when the player hits the max upgrade level and then collects the weapon upgrade again the weapon resets to the lowest level. Basically it goes:
firepower = 3, collect upgrade, firepower = 0
Here is the script I’m using:
void OnTriggerEnter (Collider other) {
if (other.tag == "Blast") {
firepower = (firepower +1 ) % 3;
Destroy(other.gameObject);
}
}
void Update ()
{
if (Input.GetButton ("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
if (firepower == 0) {
Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
audio.Play ();
}
if (firepower == 1) {
Instantiate (duoshot, duoSpawn.position, duoSpawn.rotation);
audio.Play ();
}
if (firepower == 2) {
Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
Instantiate (duoshot, duoSpawn.position, duoSpawn.rotation);
audio.Play ();
}
}
}
How do I keep my weapon from downgrading when the player collects a power up and the weapon is already at maximum fire power?