Space Shooter: How to Set a Time Limit for Powerup Duration?
I'm expanding more on the Space Shooter tutorial. I'm actually trying what another user did on this website and that is create a powerup the spaceship can collide with and turn invincible (i.e. not get killed by incoming asteroids and enemy ships). This is done by disabling the mesh collider in the OnTriggerEnter function. When the Player ship collides with the powerup, it disables the Player ship's mesh collider (that works fine). The problem is, I'm having trouble with re-enabling the mesh collider when a specific number of seconds has passed. (I'm thinking about the invincibility powerup lasting for about 20 seconds). I want to be able to re-enable the mesh collider of the Player spaceship when 20 seconds has gone by. Here's my script. What am I doing wrong?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PowerUp_invincibility : MonoBehaviour {
public GameObject[] powerUps;
public Vector3 powerUpValues;
public int powerUpCount;
public float powerUpSpawnWait;
public float powerUpStartWait;
public float powerUpWaveWait;
float time=20;
public bool gameOver;
public MeshCollider invincible;
// Use this for initialization
void Start ()
{ gameOver = false;
StartCoroutine (PowerUpWaves());
invincible = GetComponent<MeshCollider> ();
}
// Update is called once per frame
void Update()
{
if (time > 0)
{
time = Time.deltaTime;
}
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
other.gameObject.GetComponent<MeshCollider> ().enabled = false;
}
if (time <= 0)
{
other.gameObject.GetComponent<MeshCollider> ().enabled = true;
}
}
IEnumerator PowerUpWaves ()
{
yield return new WaitForSeconds (powerUpStartWait);
while (true)
{
for (int i = 0; i < powerUpCount; i++)
{
GameObject powerUp = powerUps [Random.Range (0, powerUps.Length)];
Vector3 powerUpPosition = new Vector3 (Random.Range (-powerUpValues.x, powerUpValues.x), powerUpValues.y, powerUpValues.z);
Quaternion powerUpRotation = Quaternion.identity;
Instantiate (powerUp, powerUpPosition, powerUpRotation);
yield return new WaitForSeconds (powerUpSpawnWait);
}
yield return new WaitForSeconds (powerUpWaveWait);
if (gameOver == true)
break;
}
}
}