Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sassy_Sailorman · Oct 21, 2015 at 09:41 AM · collisioncolliderloadlevel

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"

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by BeauWorlds · Oct 22, 2015 at 08:23 AM

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 :) 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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sassy_Sailorman · Oct 22, 2015 at 08:39 AM 0
Share

Thankyou so much but i found another way to do it before your reply. I tried it though and it works too. Thanks so much!

avatar image npatch · Oct 22, 2015 at 11:07 AM 0
Share

If you're gonna do string comparisons then better yet, add the enemies to their own layer and just create a layer mask which ignores everything BUT the layer of enemies. Also if I'm not mistaken Unity's collision detection and physics systems are way more optimized..they don't really calculate distances so much as use techniques to pinpoint things in the area you want. So ins$$anonymous$$d of doing things Unity does for you anyway, try to do it its way to ease your life.

avatar image
0

Answer by npatch · Oct 21, 2015 at 12:00 PM

http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision with cloth not working,Cloth component not working 0 Answers

How to get the points of 2 collided objects 0 Answers

How do I “freeze” an object after a collision? 3 Answers

Camera Collision Not Working 0 Answers

Is there any equivalent to composite collider in 3d 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges