// Update is called once per frame void Update() { inContact = false; if (collision == CollisionType.AABB) { for (int i = 1; i < collisionObjects.Length; i++) { if (CollisionDetection.AABBCollision(collisionObjects[0], collisionObjects[i])) { inContact = true; indexNumber = i; } } } else { for (int i = 1; i < collisionObjects.Length; i++) { if (CollisionDetection.CircleCollision(collisionObjects[0], collisionObjects[i])) { inContact = true; indexNumber = i; } } } if (inContact) { collisionObjects[indexNumber].color = Color.red; collisionObjects[0].color = Color.red; } else { for (int i = 0; i < collisionObjects.Length; i++) { collisionObjects[i].color = Color.white; } } if (Input.GetKeyDown(KeyCode.Alpha1) && collision != CollisionType.AABB) { collision = CollisionType.AABB; } else if (Input.GetKeyDown(KeyCode.Alpha2) && collision != CollisionType.Circle) { collision = CollisionType.Circle; } }
// Update is called once per frame void Update() { // Switches collision type on input if (Input.GetKeyDown(KeyCode.Alpha1)) { collisionType = CollisionType.AABB; } else if (Input.GetKeyDown(KeyCode.Alpha2)) { collisionType = CollisionType.Circle; } // Keeps track of all objects that are colliding this frame List <GameObject> collidingObjects = new List <GameObject>(); switch (collisionType) { case (CollisionType.AABB): for (int i = 0; i < obsticles.Length; i++) { // If there's a collision, add the colliding objects to the list if (CollisionDetection.AABBCollision(player, obsticles[i])) { // Makes sure the player object doesn't get added multiple times if (!collidingObjects.Contains(player)) { collidingObjects.Add(player); } collidingObjects.Add(obsticles[i]); } } break; case (CollisionType.Circle): for (int i = 0; i < obsticles.Length; i++) { // If there's a collision, add the colliding objects to the list if (CollisionDetection.CircleCollision(player, obsticles[i])) { // Makes sure the player object doesn't get added multiple times if (!collidingObjects.Contains(player)) { collidingObjects.Add(player); } collidingObjects.Add(obsticles[i]); } } break; } if (collidingObjects.Contains(player)) { player.GetComponent <SpriteRenderer>().color = Color.red; } else { player.GetComponent <SpriteRenderer>().color = Color.white; } for (int i = 0; i < obsticles.Length; i++) { if (collidingObjects.Contains(obsticles[i])) { obsticles[i].GetComponent <SpriteRenderer>().color = Color.red; } else { obsticles[i].GetComponent <SpriteRenderer>().color = Color.white; } } }
// Update is called once per frame void Update() { // Bring player to Game Over screen if lives run out if (lives <= 0) { SceneManager.LoadScene(2); } // Only run collisions if player isn't currently invincible if (!isInvincible) { // Iterate through all asteroids for (int i = 0; i < asteroids.Count; i++) { // If a Collision is detected destroy the asteroid and remove a life if (detector.CircleCollision(player, asteroids[i])) { Destroy(player); lives--; indices.Add(i); Destroy(asteroids[i]); player = Instantiate(ship, Vector3.zero, Quaternion.identity); audio[0].Play(); GracePeriod(5); } // Go through each index of the destroyed items and remove it from asteroids foreach (int index in indices) { asteroids.RemoveAt(index); } indices.Clear(); } } // Check Collisions for Powerups if (activePowerup != null) { if (detector.CircleCollision(player, activePowerup)) { Destroy(activePowerup); isPowerupActive = false; GracePeriod(5); audio[1].Play(); } } // If there are no asteroids left on screen spawn more but // add another asteroid every wave to increase difficulty if (asteroids.Count == 0) { asteroidCount++; for (int i = 0; i < asteroidCount; i++) { switch (Random.Range(0, 4)) { case 0: asteroid = asteroid1; break; case 1: asteroid = asteroid2; break; case 2: asteroid = asteroid3; break; case 3: asteroid = asteroid4; break; } asteroids.Add(Instantiate(asteroid, new Vector3(Random.Range(0, totalCamWidth / 2), Random.Range(0, totalCamHeight / 2), 0), Quaternion.identity)); } // Random Chance to bring in a Power Up if (Random.Range(0, 10) < 5 && !isPowerupActive && !isInvincible) // 50% chance, No other power ups on screen, player isn't already protected { activePowerup = Instantiate(powerup, new Vector3(Random.Range(0, totalCamWidth / 2), Random.Range(0, totalCamHeight / 2), 0), Quaternion.identity); isPowerupActive = true; } GracePeriod(2); } // Timer for Player Invincibility if (isInvincible) { if (timer > 0) { timer -= Time.deltaTime; } else { isInvincible = false; player.GetComponent <SpriteRenderer>().color = new Color(255, 255, 255); } } }
// Update is called once per frame void Update() { // Loops through every asteroid and if the bullet hits an // asteroid the asteroid will break and then add it's index // into the destroyed item's list for (int i = 0; i < asteroids.Count; i++) { if (detector.CircleCollision(gameObject, asteroids[i])) { indices.Add(i); Destroy(asteroids[i]); CollisionManager.score += 20; Destroy(gameObject); } // Go through each index of the destroyed items and remove it from asteroids foreach (int index in indices) { // Get the Asteroid Variant Vector3 size = asteroids[index].transform.localScale; if (asteroids[index].name == asteroidObject1.name + "(Clone)") { asteroidObject = asteroidObject1; } else if (asteroids[index].name == asteroidObject2.name + "(Clone)") { asteroidObject = asteroidObject2; } else if (asteroids[index].name == asteroidObject3.name + "(Clone)") { asteroidObject = asteroidObject3; } else if (asteroids[index].name == asteroidObject4.name + "(Clone)") { asteroidObject = asteroidObject4; } // Remove the Asteroid from the list and then check if it's a stage 1 asteroid asteroids.RemoveAt(index); // When it's a stage 1 asteroid the asteroid should split into two more asteroids if (size == asteroidObject.transform.localScale) { for (int x = 0; x < 4; x++) { GameObject newAsteroid = Instantiate(asteroidObject, transform.position, Quaternion.identity); newAsteroid.transform.localScale /= 2; asteroids.Add(newAsteroid); } } } indices.Clear(); } // Calculations for applying acceleration to the bullet acceleration = direction * accelerationRate; velocity += acceleration; bulletPosition += velocity; // Conditionals for screen wrapping on the x-axis if (transform.position.x > totalCamWidth / 2 && !(transform.position.x > -totalCamWidth / 2 && transform.position.x < totalCamWidth / 2)) { bulletPosition.x = -totalCamWidth / 2; } if (transform.position.x < -totalCamWidth / 2 && !(transform.position.x > -totalCamWidth / 2 && transform.position.x < totalCamWidth / 2)) { bulletPosition.x = totalCamWidth / 2; } // Conditionals for screen wrapping on the y-axis if (transform.position.y > totalCamHeight / 2 && !(transform.position.y > -totalCamHeight / 2 && transform.position.y < totalCamHeight / 2)) { bulletPosition.y = -totalCamHeight / 2; } if (transform.position.y < -totalCamHeight / 2 && !(transform.position.y > -totalCamHeight / 2 && transform.position.y < totalCamHeight / 2)) { bulletPosition.y = totalCamHeight / 2; } transform.position = bulletPosition; }