How do I make a zone that will load the following level once all enemies are enclosed within?

I’m building a very simple game that involves luring enemies into special “red zones” (just red colour painted on terrain). My question is how can i make it so that once all enemies have been secured in a “red zone”, i can load the next level, and on the final level, print a message saying “you win”

Hi Sassy_Sailorman (nice name!)

There are a few ways you could do this, with colliders, or an OverlaySphere as npatch has suggested, I would have a script that gets all objects by the tag or “Enemy” (for example) and calculate their distances, if all distances are within the required distance then call Application.LoadLevel

I have written an example of this for you:

using UnityEngine;
using System.Collections;
    
public class EnemyDetection : MonoBehaviour {
	public float distance = 10;

	public int checkInterval = 3;

	public int levelToLoad = 1;

	public Transform[] redZones;

	void Start(){
		// Sets an invoke for CheckDistancesto be called every checkInterval seconds
		InvokeRepeating("CheckDistances", 0, checkInterval);
	}

	void CheckDistances(){
		//Gets all gameobjects with the tag Enemy
		GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
		float dist;

		// checks every enemy to see if it is closer than distance
		foreach(GameObject enemy in enemies){
			bool inside = false;
			foreach(Transform zone in redZones){
				dist = Vector3.Distance(enemy.transform.position, transform.position);

				if(dist < distance){
					inside = true;
				}
			}
			if(!inside){
				return; // if an enemy is not inside of any of the zomes then stop the function
			}
		}
		// if all enemies are closer than distance, the function will not have been stopped so
		// will arrive at this point and will load the chosen level
		Application.LoadLevel(levelToLoad);
	}
}

Place this c# script onto a game object in the scene, in the array redZones place game objects at the center of your red zones.

The distance that the enemies must be from the game object is set by the distance variable

Then create the tag “Enemy” and assign the tag to all your enemies

When all the enemies (objects with the tag enemy) are closer than “distance” to a red zone, level 1 will be loaded.

Don’t forget you need to set level 0 and 1 in the Build Settings

For the you win, just put a ui with text saying you win on the screen, or you could make a nice image in photoshop or gimp and assign this image to a UI Image

I hope this helps, if you want me to explain anything in more detail or if you have any questions please let me know :slight_smile: I have not tried this code in unity, but I’ve read and re-read it, it should work, but let me know if it does anything wrong.

Beau C