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
7
Question by SparkGames · Sep 08, 2013 at 09:12 AM · gameobjectplayerfindactivation

Finding GameObjects within a radius?

i want to find the gameobjects around my player. what i want to do is find all the gameobjects within a certain area around my player and activate them. i am working on an openworld game. please help.

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

Answer by fafase · Sep 08, 2013 at 11:12 AM

Do yourself a favor and make it simple, use http://docs.unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html

This will return all colliders within radius. From there you can access gameObject.

EDIT:

If the objects are not active you may want to create an array and store all objects at Start, then you can perform:

 Transform [] array;
 void GetInactiveInRadius(){
    foreach (Transform tr in array){
      float distanceSqr = (transfor.position - tr.position).sqrMagnitude;
      if(distanceSqr < rangeSqr)
       tr.gameObject.SetActive(true);
    }
 }
Comment
Add comment · Show 13 · 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 meat5000 ♦ · Sep 08, 2013 at 11:14 AM 1
Share

Bugger. Beat me to it.

avatar image SparkGames · Sep 08, 2013 at 11:15 AM 0
Share

err one question...it returns all the colliders..ok...well...all the gameobjects in my scene are at first NOT active evertything is set to not active..will it still return colliders?? i hope it does cuz i thot if the object is set to setactive ( false ) the colliders hide with it

avatar image fafase · Sep 08, 2013 at 11:16 AM 5
Share

The force is strong in you but speed you still lack.

avatar image SparkGames · Sep 08, 2013 at 11:23 AM 0
Share

i dont understand.. i am new to unity....well...will overlap sphere return colliders of even diabled objects?? becuz my sole purpose is to enable the objects in within a certain area around the player

avatar image fafase · Sep 08, 2013 at 11:24 AM 0
Share

There could be workaround, first you have objects active, colliders as IsTrigger but all other scripts are inactive and using OverlapSphere, you set them to active.

Other way is using Linq:

 float distance;
 
 var transformArray = (GameObject)FindObjectsWOfType(typeof(GameObject))
     .Select(go => go.transform)
     .Where(t => Vector3.Distance(t.position - transform.position) < distance
     .ToArray();

This will return an array of objects within distance. But this is pretty slow and if you do that once in a while, you can get away with it, if you do that in the Update, get ready for some slow motion effect.

Not sure though it works on inactive object.

$$anonymous$$aybe your best solution is an array in which you store them all at the beginning, then inactive the one you want, and later on perform the check:

 void GetInactiveInRadius(){
    foreach (GameObject obj in array){
      if(Vector3.Distance(transfor.position,obj.transform.position) < distance)
       obj.SetActive(true);
    }
 }
Show more comments
avatar image
5

Answer by sagivo · Jun 04, 2016 at 03:53 PM

simply use Physics.OverlapSphere Physics.OverlapSphere (someposition, someradius);

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

22 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

Related Questions

Alternatives to GameObject.Find(); 1 Answer

Finding Children question 3 Answers

Find all gameObjects with same tag 1 Answer

Search for specific entity in an array? 1 Answer

Organize around inactive GameObjects not being findable 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