Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by DoubleUU · Mar 28 at 06:23 AM · overlapsphereobject pool

OverlapSphere Detecting Colliders Outside of Range (Pooled Objects)

I have a pool of 10 enemy objects that get spawned from one of 3 random locations around a central point.

I have towers which target the incoming enemies using OverlapSphere. I'm not using a layer mask at the moment.

After some time running, towers will start picking up enemies which are being spawned far outside of their OverlapSphere range. I'm looking for targets like this:

 private void LookForTargets() {
 
         if (targetEnemy != null && !targetEnemy.gameObject.activeInHierarchy) {
             Debug.Log($"{name} target {targetEnemy.gameObject.name} is now inactive");
             targetEnemy = null;
         }

         float targetRadius = 5f;
         Collider[] colliders = Physics.OverlapSphere(transform.position, targetRadius);
         foreach (Collider collider in colliders) {
 
             Enemy potentialTargetEnemy = collider.GetComponent<Enemy>();
             if (potentialTargetEnemy != null) {
 
                 // Found an enemy.
                 // Previously pooled objects are getting picked up when they are reactivated
                 // even though the should have been outside of the target radius.
                 float distanceToNewEnemy = Vector3.Distance(transform.position, potentialTargetEnemy.transform.position);
                 if (distanceToNewEnemy > targetRadius) {
                     Debug.Log($"{name} distance to {potentialTargetEnemy.gameObject.name} = {distanceToNewEnemy}");
                     continue;
                 }
                 
                 if (targetEnemy == null) {
                     SetTargetEnemy(potentialTargetEnemy);
                 } else {
                     float distanceToCurrentEnemy = Vector3.Distance(transform.position, targetEnemy.transform.position);
                     if (distanceToNewEnemy < distanceToCurrentEnemy) {
                         SetTargetEnemy(potentialTargetEnemy);
                     }
                 }
             }
         }
 
     }

It can run fine for a period of time, then suddenly one tower or another will start picking up colliders way outside of their range. I set it up so that each spawn point should only be targeted by one set of towers. These two towers finished a wave and then on the next wave that spawned generated the following messages. The specified enemy game object was NOT in the previous wave:

 pfArrowTower(Clone)3 distance to pfEnemyMeteorite(Clone)_1 = 32.51066
 UnityEngine.Debug:Log (object)
 Tower:LookForTargets () (at Assets/Scripts/Tower.cs:62)
 Tower:HandleTargetting () (at Assets/Scripts/Tower.cs:39)
 Tower:Update () (at Assets/Scripts/Tower.cs:18)
 
 pfArrowTower(Clone)4 distance to pfEnemyMeteorite(Clone)_1 = 30.09656
 UnityEngine.Debug:Log (object)
 Tower:LookForTargets () (at Assets/Scripts/Tower.cs:62)
 Tower:HandleTargetting () (at Assets/Scripts/Tower.cs:39)
 Tower:Update () (at Assets/Scripts/Tower.cs:18)

It spawned on the other side of the map and is clearly outside of the radius supplied to OverlapSphere. Why would it get picked up?

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
0
Best Answer

Answer by DoubleUU · Mar 29 at 03:16 AM

My error was in the way objects were spawned from the pool:

         objectToSpawn.SetActive(true);
         objectToSpawn.transform.position = position;
         objectToSpawn.transform.rotation = rotation;

should have been:

         objectToSpawn.transform.position = position;
         objectToSpawn.transform.rotation = rotation;
         objectToSpawn.SetActive(true);

Apparently it was possible for the target check to detect the object just before it was relocated.

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

177 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

Related Questions

Overlapsphere issue with laymask 0 Answers

Playable was not Disposed 1 Answer

Why does Physics.OverlapSphere not always return the same number of colliders? 1 Answer

Procedural Object Placement in games like RUST and DayZ? 0 Answers

Object Pooling Causing Stutter 1 Answer


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