bool CheckForCollision(SpriteRenderer objA, SpriteRenderer objB, CollisionCheckMethod collisionCheck) { bool isHitting = false; switch (collisionCheck) { case CollisionCheckMethod.AABB: if (objB.bounds.min.x < objA.bounds.max.x && objB.bounds.max.x > objA.bounds.min.x && objB.bounds.max.y > objA.bounds.min.y && objB.bounds.min.y < objA.bounds.max.y) { isHitting = true; } break; case CollisionCheckMethod.Circle: if (Vector3.Distance(objA.bounds.center, objB.bounds.center) < (objA.bounds.size.magnitude + objB.bounds.size.magnitude) / 2f) { isHitting = true; } break; } return(isHitting); }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.C)) { switch (checkMethod) { case CollisionCheckMethod.AABB: checkMethod = CollisionCheckMethod.Circle; break; case CollisionCheckMethod.Circle: checkMethod = CollisionCheckMethod.AABB; break; } } bool isPlayerHit = false; for (int i = 1; i < objects.Count; i++) { if (CheckForCollision(objects[0], objects[i], checkMethod)) { objects[i].color = Color.red; isPlayerHit = true; //objects[0].color = Color.red; } else { objects[i].color = Color.white; //objects[0].color = Color.white; } } if (isPlayerHit) { objects[0].color = Color.red; } else { objects[0].color = Color.white; } }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.C)) { switch (checkMethod) { case CollisionCheckMethod.AABB: checkMethod = CollisionCheckMethod.Circle; break; case CollisionCheckMethod.Circle: checkMethod = CollisionCheckMethod.AABB; break; } } bool isPlayerHit = false; for (int i = 2; i < objects.Count; ++i) { if (CheckForCollision(objects[0], objects[i], checkMethod)) { objects[i].color = Color.red; isPlayerHit = true; //objects[0].color = Color.red; } else { objects[i].color = Color.white; //objects[0].color = Color.white; } } if (isPlayerHit) { objects[0].color = Color.red; //GameObject ship = GameObject.Find("Ship"); //Vehicle vehicle = ship.GetComponent<Vehicle>(); //vehicle.vehiclePosition = Vector3.zero; count++; //count is set so high because the ship can be in the asteroid if (count == 1) { shipLife++; if (shipLife == 1) { Text t = life1.GetComponent <Text>(); t.text = " "; } } if (count == 150) { shipLife++; if (shipLife == 2) { Text t = life2.GetComponent <Text>(); t.text = " "; } } if (count == 300) { shipLife++; if (shipLife == 3) { // restartes the game SceneManager.LoadScene(SceneManager.GetActiveScene().name); } } } else { objects[0].color = Color.white; } }