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 /
  • Help Room /
avatar image
0
Question by ez06 · Sep 09, 2015 at 03:27 AM · performance

Most efficient way of detecting closest objects around?

I'm considering how to detect the closest object around my hero. This is gonna be called for every frame so I really need the most efficient way.

Should I do a For loop of my array of objects and calculate the distance of each object to my hero and choose the smallest value?

Or should I use other means such as raycasts?

I'm targeting mobile so every bits of CPU saving helps.

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
1

Answer by Pangamini · Sep 09, 2015 at 08:18 AM

Physics engine inside unity keeps colliders in some structure that makes if fast to query objects in an area (99% sure it's an octree). So when you are using Physics.OverlapSphere, it should be optimized and fast. This will only work with colliders though, not with any GO / Transform (since the octree is part of physics engine) and if you plan to move them around, you'd better have a kinematic rigidbody there as well.

If you need / want, you can implement your own octree and fill it with your objects. This can be handy if you want to have multiple parallel "worlds" populated with objects where you can search, or you simply don't want to use colliders and physics and waste some unity layers for filtering them out.

Comment
Add comment · Show 2 · 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 Pangamini · Sep 09, 2015 at 08:21 AM 0
Share

Also don't forget that objects returned by OverlapSphere are deter$$anonymous$$ed using collider and sphere's bounding boxes, so it's just a rough list. It will contain all objects inside the sphere, but may contain some more, so you should do another, more expensive pass on the returned list

avatar image ez06 · Sep 09, 2015 at 10:58 AM 0
Share

Hi there, thanks for your response. None of the objects around my hero have rigidbodies. I want to keep them free of physics to have complete control over them. Can I just add a trigger collider to my objects and do "collider.gameobject.transform.position"?

avatar image
0

Answer by Eno-Khaon · Sep 09, 2015 at 04:26 AM

If you're looking for pure efficiency, you might want to consider using a trigger area collider sphere around your character.

As a script on that, you would use OnTriggerEnter() to compile a running List<> of objects as your character gets near them and then OnTriggerExit() to clear them out of the list.

Then, you'll only need to check whichever objects are contained within a close range of you to determine which is closest after that.

If you have extremely few objects in the scene or they're all generally going to be very, very far away (and you still want to know which is closest), then you might consider alternative methods instead.

Edit: Fixed formatting for "List<>"

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

31 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

Related Questions

Is instantiating empty gameObjects heavy on the CPU? ( Object pooling) 1 Answer

Is using multiple cameras, culling masks, and layers expensive on mobile? 1 Answer

Performance issue new project vs everything deleted in old project 0 Answers

How to optimise image effects on Android? 1 Answer

Multi-core PCs and performance. 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