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 SoloChristian · Oct 01, 2010 at 02:01 AM · layersradarselections

Select by Layer for a radar system

I was looking at the Dastardly Banana Radar sample and was wondering if you could do selections by Layers instead of Tags. My Networking system is using the code and it would be bulky at best to add radar to this.

But I do easily have the option to use Layers. Is there a fast / efficient way to do this?

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

Answer by skovacs1 · Oct 01, 2010 at 06:02 PM

To my knowledge, there is no fast/efficient way to get all objects on a layer, depending on your definition of fast efficient.

Finding

Find is slow and only finds one object. FindObjectsOfType is very slow.

FindGameObjectsWithTag is really the only fast/efficient option. If you setup your tagging correctly, this is really quite simple. Moreover, since you can have only 32 layers, while you can have n tags, tags are more extensible.

You could loop through the tagged game objects that are returned and only act on ones that match certain criteria.

for(var foo : GameObject in GameObject.FindGameObjectsWithTag("bar"))
    if(foo.layer == 10) doSomething(foo);

But regardless, you'd probably have to do multiple passes as you likely care about several differently tagged objects, so simply tagging properly avoids even having to check layers or any other such nonesense.

Static Caching

Find functions are really only slow and inefficient when called. If you don't call them very often, they're less horrible. If you were to call your Find functions once at the beginning to grab everything, storing only stuff on a given layer into some sort of array, it would only be slow/costly once. If your objects don't change layers, then you would have your array of all objects on a given layer. Otherwise, every time anything changes layers, you'll have to rebuild the array.

Dynamic Caching

You could maintain an array of the things you care about and then use these things in your radar. Add things when they enter the area and remove them when they leave the area. This is more easily done with a System.Collections collection like a List or SortedList.

You could do this with a trigger or collider to represent the radar area. This depends on Unity's messaging system and, if you aren't using the physics system for anything else, this could bring some overhead.

You could do this by a script added to things that show up on the radar that will check their distance to the target whenever their position changes and manage their existence in the radar array. This generally can be optimized to at worst have the cost of a Vector3 subtraction, Vector3.sqrMagnitude and a floating point comparison for every object that has this script every time it is checked.

You could do this by a radar script which raycasts to each of the things you care about up to the radar distance and then adds them to your radar if they were hit. You can layer mask if you don't care about visibility. This requires that each of the objects has a collider.

Alternative ideas

If you are just going to use this for rendering the radar, you could use a radar camera (maybe orthographic) with a layer mask and maybe some replaced shaders or image effects or something.

Comment
Add comment · Show 1 · 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 SoloChristian · Oct 03, 2010 at 02:35 AM 0
Share

Thanks everyone These answers were very helpful. :)

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

No one has followed this question yet.

Related Questions

MiniMap Border! 1 Answer

Text Animation is always stay behind the Default canvas 3 Answers

Raycast ignoring layers that it should not 2 Answers

Can't get TrailRenderer behind gameObject. 1 Answer

Simple Quick Questions(about buttons and inspector) 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