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 theunsigned · Feb 25, 2020 at 12:59 PM · collisionphysicscollidertriggeroverlapsphere

overlapsphere to destroy NPCs on exit

Hi, I'm new to overlapshpere and have been using it to activate spawners in the player's vicinity but I thought I could also use it to destroy NPCs that wander way out of range. I have a population manager script that creates lists of NPCs and has the overlap sphere on it; this is attached to my player.

Some issues I'm running into are:

  1. how to use overlapsphere to detect if an NPC has left exited the trigger zone, since the list of NPCs is changing all the time and overlapsphere seems dependent on arrays

  2. how to differentiate between different sphere colliders on the same object. I have two sphere colliders testing for different ranges, I have assigned them to different variables, but I'm having difficulty differentiating between them in the code. I would like to disable mesh renderers if the NPC leaves the closest sphere and destroy if it leaves the larger one to help with performance.

  3. Is it better to simply use a sphere collider exit instead of overlapsphere to test for exiting NPCs from the list of NPCs?

    I appreciate any guidance, general or specific, to help me understand overlapsphere and how to properly implement it.

    Here's the code I have so far, which creates lists and does the overlapsphere activation/deactivation of the spawners:


 public class populationmanager : MonoBehaviour
 {
     //layermasks
     public LayerMask spawner, allnpc;
 
     public List<GameObject> allnpcs;
 
     public SphereCollider enablesphere, disablesphere;
     Collider[] spawners, npcs;
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.GetComponent<spawner>() != null)
         {
         ObjectComponentState(other, true);
 }
     }
 
     private void OnTriggerExit(Collider other)
     {
         ObjectComponentState(other, false);
         if (other.GetComponent<spawner>() != null)
         {
             other.GetComponent<spawner>().StopAllCoroutines();
         }
     }
 
     private void ObjectComponentState(Collider other, bool state)
     {
 
         if (other.GetComponent<spawner>() != null)
         {
 
             other.GetComponent<spawner>().enabled = state;
         }
 
     }
 
     private void FixedUpdate()
     {
         spawners = Physics.OverlapSphere(transform.position, enablesphere.radius, spawner);
 
         for (int i = spawners.Length - 1; i > -1; i--)
         {
             ObjectComponentState(spawners[i], true);
         }
 
     }
 
 }
 


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

1 Reply

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

Answer by unity_ek98vnTRplGj8Q · Feb 25, 2020 at 05:25 PM

  1. If you want to use overlap sphere, you would have to maintain a list of active objects and then check every frame to see if an object in that list did not show up in the next sphere cast. This is definitely not an efficient way to do this.

  2. Yes using OnTriggerExit would be much better as calculating collisions is much less expensive than a spherecast.

  3. If you only have 1 spawn area, then it would be most efficient just to get rid of the large colliders all together and check the distance from each object to the center point of the spawn area

Comment
Add comment · Show 8 · 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 theunsigned · Feb 25, 2020 at 07:19 PM 0
Share

Thank you! Sometimes it just takes writing out the problem and hearing back from more experienced people such as yourself to have things "click."

Since I am making an open world game with many different spawners, I will likely use the sphere overlap for that but I will keep your alternative in $$anonymous$$d. Thank you for clearing up with ontriggerexit piece!

avatar image theunsigned · Feb 25, 2020 at 08:10 PM 0
Share

I'm curious if you could expand on number 3 a bit because, though my spawn area is around my moving player, it really does amount to only one area of spawning at a time. Are you saying that it is better for performance to check each spawner's distance to the moving player each frame than to check with an overlapsphere?

avatar image unity_ek98vnTRplGj8Q theunsigned · Feb 25, 2020 at 09:50 PM 0
Share

Yes, here's why:
OverlapSphere is a physics query, it goes into Unity's physics system and uses some complex algorithms to find exactly what objects are in or touching the given sphere. This could potentially check against every object in your scene including invisible objects, terrain, scene details, etc. which must each take into account their OWN colliders to calculate overlap.

For a distance check all you are doing is doing some simple Vector addition. Yes you are doing it for each NPC, but the overhead here is so greatly reduced that it's ok. THAT BEING SAID, depending on how your game works it is possible that OverlapSphere is better. If you have a significant number (several hundred, maybe several thousand) of NPC's then maybe it is worth revisiting exactly how you are doing this and investigating more effective optimizations

avatar image unity_ek98vnTRplGj8Q unity_ek98vnTRplGj8Q · Feb 25, 2020 at 09:56 PM 0
Share

It doesn't matter whether or not the player is moving for a distance check because you don't have to do any extra work to find the position values, they are already calculated for you each frame. However just having colliders that move around in the world (even if you don't use them) has an associated overhead because these colliders must be maintained in Unity's physics system (moving a collider requires a lot of extra calculation behind the scene).

Show more comments

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

264 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to design an Archer Board Scoring System 1 Answer

Trigger to some, Collider to others 1 Answer

Compound collider object behaving as a whole? 1 Answer

Detect collider collision ,identify and access colliding colliders 0 Answers

How having correct bounce on a rotating pinwheel(it's more complicated)? 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