{"id":4839,"date":"2018-10-31T15:32:19","date_gmt":"2018-10-31T07:32:19","guid":{"rendered":"https:\/\/unitycoder.com\/blog\/?p=4839"},"modified":"2018-11-02T16:10:48","modified_gmt":"2018-11-02T08:10:48","slug":"find-nearby-objects-using-cullinggroup","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2018\/10\/31\/find-nearby-objects-using-cullinggroup\/","title":{"rendered":"Find Nearby Objects using CullingGroup"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4841\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2018\/10\/31\/find-nearby-objects-using-cullinggroup\/cullinggroup-nearby-objects\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/cullinggroup-nearby-objects.gif?fit=580%2C286&amp;ssl=1\" data-orig-size=\"580,286\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"cullinggroup-nearby-objects\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/cullinggroup-nearby-objects.gif?fit=580%2C286&amp;ssl=1\" class=\"alignnone size-full wp-image-4841\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/cullinggroup-nearby-objects.gif?resize=580%2C286&#038;ssl=1\" alt=\"\" width=\"580\" height=\"286\" \/><\/p>\n<p>Saw this tip at #UniteKL18, fast way to get nearby objects using <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/CullingGroup.html\" target=\"_blank\" rel=\"noopener\">CullingGroup<\/a>!<br \/>\n(main image with 5000 gameobjects, searching objects within 3m radius)<\/p>\n<p><strong>example source code:<br \/>\n<\/strong><a href=\"https:\/\/github.com\/UnityCommunity\/UnityLibrary\/blob\/master\/Assets\/Scripts\/Docs\/UnityEngine\/CullingGroupExample.cs\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/UnityCommunity\/UnityLibrary\/blob\/master\/Assets\/Scripts\/Docs\/UnityEngine\/CullingGroupExample.cs<\/a><\/p>\n<p><strong>my example project:<\/strong><br \/>\n<a href=\"https:\/\/github.com\/unitycoder\/GetNearestObjectsWithCullingGroup\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/unitycoder\/GetNearestObjectsWithCullingGroup<\/a><\/p>\n<p><strong>Some notes on that:<\/strong><br \/>\n&#8211; You need to have MainCamera in the scene (as it uses that in &#8220;cullGroup.targetCamera = Camera.main;&#8221;)<br \/>\n&#8211; You need to set proper bounds for your objects, i&#8217;m currently doing that inside that for-loop (see <span class=\"pl-smi\">b<\/span>.<span class=\"pl-smi\">position=..<\/span> and <span class=\"pl-smi\">b<\/span>.<span class=\"pl-smi\">radius=..)<br \/>\n&#8211; i&#8217;m using array of Renderers to keep track of spawned objects (just to make it easy to change color without GetComponent&lt;Renderer&gt;), but you might want to use List, so that can add\/remove objects there easily.<br \/>\n&#8211; I&#8217;m spawning objects on 2D circle, but this would work in 3D too<br \/>\n<\/span><\/p>\n<p>&#8212;<\/p>\n<p><strong>TODO<\/strong><br \/>\n&#8211; Compare speed with OnTriggerEnter, Physics.OverlapSphere, BruteForce LINQ distances query, KD\/QuadTree<br \/>\n&#8211; Test how this works with moving objects? (to get closest enemies for player..)<br \/>\n&#8211; Test how to get closests objects to 2 points? (probably cannot easily?)<\/p>\n<p>&#8212;<\/p>\n<p>Can also check which objects are outside camera view:<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4848\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2018\/10\/31\/find-nearby-objects-using-cullinggroup\/objects-outside-fov\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/objects-outside-fov.jpg?fit=680%2C198&amp;ssl=1\" data-orig-size=\"680,198\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"objects-outside-fov\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/objects-outside-fov.jpg?fit=680%2C198&amp;ssl=1\" class=\"alignnone size-full wp-image-4848\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/objects-outside-fov.jpg?resize=680%2C198&#038;ssl=1\" alt=\"\" width=\"680\" height=\"198\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/objects-outside-fov.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/objects-outside-fov.jpg?resize=300%2C87&amp;ssl=1 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Saw this tip at #UniteKL18, fast way to get nearby objects using CullingGroup! (main image with 5000 gameobjects, searching objects within 3m radius) example source code: https:\/\/github.com\/UnityCommunity\/UnityLibrary\/blob\/master\/Assets\/Scripts\/Docs\/UnityEngine\/CullingGroupExample.cs my example project: https:\/\/github.com\/unitycoder\/GetNearestObjectsWithCullingGroup Some notes on that: &#8211; You need to have MainCamera in the scene (as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4841,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Find Nearby Objects using CullingGroup #UnityTips","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[804,3],"tags":[1064,1063,433,444,447,463,445,1065],"class_list":["post-4839","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripts","category-unity3d","tag-closest","tag-cullinggroup","tag-fast","tag-find","tag-nearest","tag-objects","tag-search","tag-transforms"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/cullinggroup-nearby-objects.gif?fit=580%2C286&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-1g3","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/4839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=4839"}],"version-history":[{"count":8,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/4839\/revisions"}],"predecessor-version":[{"id":4850,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/4839\/revisions\/4850"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/4841"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=4839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=4839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=4839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}