// Update is called once per frame void Update() { currentLife += Time.deltaTime; transform.localPosition += travellingNormal * speed * Time.deltaTime; // travel if (world.existingSentries.Count > 0) // test collision with every existing sentry { for (int i = 0; i < world.existingSentries.Count; i++) { StationarySentry sentry = world.existingSentries[i]; bool isCollide = world.ProcessCollision(transform, sentry.gameObject.transform); if (isCollide) { //Debug.Log("Player Bullet Hit"); sentry.enabled = false; //world.existingSentries.Remove(world.existingSentries[i]); //GameObject.Destroy(sentry.gameObject); } //else // Debug.Log("Player Bullet not hit"); } } if (world.asteroids.Count > 0) { for (int j = 0; j < world.asteroids.Count; j++) { Asteroid ast = world.asteroids[j]; bool isCollide = world.ProcessCollision(transform, world.asteroids[j].gameObject.transform); if (isCollide) { world.asteroids.Remove(world.asteroids[j]); //Debug.Log("Asteroid destroyed"); GameObject.Destroy(ast.gameObject); } } } if (world.giantAsteroids.Count > 0) { for (int i = 0; i < world.giantAsteroids.Count; i++) { GiantAsteroid ga = world.giantAsteroids[i]; for (int j = 0; j < ga.parts.Count; j++) { Asteroid ast = ga.parts[j]; bool isCollide = world.ProcessCollision(transform, ga.parts[j].gameObject.transform); if (isCollide) { ga.parts.Remove(ga.parts[j]); //Debug.Log("Giant Asteroid part destroyed"); GameObject.Destroy(ast.gameObject); } } if (ga.parts.Count <= 0) { world.giantAsteroids.Remove(world.giantAsteroids[i]); GameObject.Destroy(ga.gameObject); } } } if (currentLife >= timeToLive) { Destroy(gameObject); } }
private void Update() { shipHealth.text = "Space Ship Health: " + health; if (health <= 0) { SetPlayMode(); warning.text = "Game over! Ship destroyed!"; health = 100; } if (Input.GetKeyDown(KeyCode.X)) { if (giantAsteroids.Count > 0) { for (int k = 0; k < giantAsteroids.Count; k++) { GiantAsteroid ga = giantAsteroids[k]; giantAsteroids.Remove(giantAsteroids[k]); GameObject.Destroy(ga.gameObject); } } } if (TheRoot != null) { Matrix4x4 i = Matrix4x4.identity; TheRoot.CompositeXform(ref i); if (isPlayTime) { TheRoot.transform.localPosition += TheRoot.transform.up * .1f; velocity = TheRoot.transform.up * .1f; timePastInstantiation += Time.deltaTime; curgiantGen += Time.deltaTime; if (curgiantGen >= giantGenInterval) { GameObject g = Instantiate(Resources.Load("Prefabs\\GiantAsteroid")) as GameObject; g.GetComponent <GiantAsteroid>().world = this; // Set spawning location g.transform.localPosition = new Vector3(Random.Range(asteroidSpawningPoint.transform.localPosition.x - 50f, asteroidSpawningPoint.transform.localPosition.x + 50f), Random.Range(asteroidSpawningPoint.transform.localPosition.y - 50f, asteroidSpawningPoint.transform.localPosition.y + 50f), Random.Range(asteroidSpawningPoint.transform.localPosition.z - 50f, asteroidSpawningPoint.transform.localPosition.z + 50f)); giantAsteroids.Add(g.GetComponent <GiantAsteroid>()); // Sets direction of travel and speed of asteroid curgiantGen = 0f; } if (timePastInstantiation >= generateInterval) { GameObject g = Instantiate(Resources.Load("Prefabs\\Asteroid")) as GameObject; g.GetComponent <Asteroid>().Initialize(this); // Set spawning location g.transform.localPosition = new Vector3(Random.Range(asteroidSpawningPoint.transform.localPosition.x - 5f, asteroidSpawningPoint.transform.localPosition.x + 5f), Random.Range(asteroidSpawningPoint.transform.localPosition.y - 5f, asteroidSpawningPoint.transform.localPosition.y + 5f), Random.Range(asteroidSpawningPoint.transform.localPosition.z - 5f, asteroidSpawningPoint.transform.localPosition.z + 5f)); // Sets direction of travel and speed of asteroid g.GetComponent <Asteroid>().SetTravellingDirection(new Vector3(Random.Range(-1, 1), Random.Range(-1, 1), Random.Range(-1, 1))); g.GetComponent <Asteroid>().SetSpeed(15f); asteroids.Add(g.GetComponent <Asteroid>()); timePastInstantiation = 0f; } } else { for (int k = 0; k < giantAsteroids.Count; k++) { GiantAsteroid ga = giantAsteroids[k]; giantAsteroids.Remove(giantAsteroids[k]); GameObject.Destroy(ga.gameObject); } for (int k = 0; k < asteroids.Count; k++) { Asteroid ast = asteroids[k]; asteroids.Remove(asteroids[k]); GameObject.Destroy(ast.gameObject); } foreach (StationarySentry sentry in existingSentries) { sentry.enabled = true; } } } }