Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Lord Simpson · Feb 10, 2014 at 11:08 AM · gameobjectsphererangeoverlapsphere

Find All objects in range (no physics)

Hi I'm building a system where an object effects all objects in its range (spherical), for objects that are rigidbodys I can use a trigger sphere collider and apply the effect on trigger stay etc, however I also have a number of gameobjects I need to also apply the effect to that don't use rigidbodys for performance reasons and I'm trying to figure out the fastest way to effect them.

The obvious solution would be to loop through all objects and see if they are in range by comparing the distance vectors square magnitude to the square of the range, but I'm hoping there's a faster solution like physics.overlapsphere but for everything?

If not the next step would be optimising the loop in which case I assume tags or layers are the fastest way to cull the list down to the objects that I require?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Phles · Feb 10, 2014 at 11:42 AM

If you really want to avoid using physics completely (no colliders) as your questions title implies you can tag all the objects you are interested in and use GameObject.FindGameObjectsWithTag(tag) and loop through the array of tagged objects checking distances.

Adding colliders and using the Physics.OverlapSphere approach is probably a better way to go though, be sure to use a layer on your objects and use Physics.OverlapSphere with a layermask or with a large radius and lots of objects it gets to be somewhat inefficient.

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
avatar image
0

Answer by wibble82 · Feb 10, 2014 at 11:21 AM

Hi There

It sounds like what you need is to have the objects you're searching for have a 'collider' attached (not a rigid body). This means they exist in the physics engines view of the world, but don't move or bounce around. In addition, in unity you can specify what layers collide with other layers, so you could keep these in a special layer to avoid them hitting other objects. These would have a memory cost, but very little extra performance cost (as they don't move, and don't collide with any of your rigid bodies).

Once that's done, Physics.OverlapSphere is what you're after!

Check out this answer for a different problem with a similar solution:

http://answers.unity3d.com/questions/231987/find-all-objects-currently-colliding-with-trigger.html

-Chris

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
avatar image
0

Answer by vuwij · Oct 30, 2016 at 07:52 AM

You can use Physics2D.CircleCastAll if you are using 2D, in 3D there is Physics.CapsuleCastAll or SphereCastAll. This sends a spherical raycast looking or any objects with Colliders. there is no way you can avoid colliders

 public void Inspect()
 {
     RaycastHit2D[] castStar = Physics2D.CircleCastAll(transform.position, inspectRadius, Vector2.zero);
     foreach (RaycastHit2D raycastHit in castStar)
     {
         if (InspectionIsInvalid(raycastHit)) continue;
 
         IInspectable[] mObj = raycastHit.collider.GetComponentsInChildren<IInspectable>();
         if (mObj.Length != 0)
         {
             Debug.Log(raycastHit.collider.name);
             foreach (IInspectable obj in mObj)
                 obj.InspectionAction(raycastHit);
             return;
         }
     }
 }

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
avatar image
0

Answer by TheReEvolutions · Jul 10, 2017 at 01:56 PM

For anyone wanting to use vector loop distance method. Hope it helps .

 var colorR : Color;  
 
 function FindInRange () : GameObject {
         var gos : GameObject[];
         gos = GameObject.FindGameObjectsWithTag("Target Objects Tag Here"); 
        
         var radiusDistance = 50; // the range of distance
         var position = transform.position; 
        
         // Iterate through them and find the objects within range 
         for (var go : GameObject in gos)  { 
             var diff = (go.transform.position - position);
             var curDistance = diff.sqrMagnitude; 
             if (curDistance < radiusDistance) { 
             go.GetComponent.<Renderer>().material.color = colorR; // effect on object
                
             } 
         } 
     
     }
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

23 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

Related Questions

How to Destroy gameObjects and deal damage within a radius except player ? 2 Answers

random limitation memory game 0 Answers

How do I reference a GameObject after finding its collider with Physics.OverlapSphere()? 1 Answer

How come certain Children don't cast an Overlapsphere? 1 Answer

Best way to load high volume of particles. 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