Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Any better way of detecting if bounds contains any gameobjects?

Discussion in 'Scripting' started by disorganized, Oct 14, 2019.

  1. disorganized

    disorganized

    Joined:
    Jan 11, 2014
    Posts:
    29
    Sorry if this should be under UI or something since it involves a drag selection box.

    Basic RTS drag selection box that checks a list of selectable units to see if any of them are within the selection bounds. If one is, then it's 'selected'. Pretty simple.
    It works just fine but I'm just wondering if anyone knows of a better way of checking to see if the bounds contains any gameObjects without having to run through the list essentially every frame of a drag select? I guess I'm just looking to optimize this whole process a little.

    Thanks in advance for any advice anyone can offer.
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Easiest thing I can think of is to reduce the rate of checking, if it does not make your setup too unresponsive/sluggish. If you do that bounds check every 10th frame for example?

    Unless you are running into performance issues I wouldn't worry too much about it unless you are already in the phase where you are just optimizing your game.
     
    disorganized and Joe-Censored like this.
  3. disorganized

    disorganized

    Joined:
    Jan 11, 2014
    Posts:
    29
    Nah, performance is fine. I'm just going through some stuff and seeing if there's room for improvement. It only happens when the user is click dragging the mouse so it's not happening literally every frame of game play.
    Seems as though there should be a simpler way to check in any objects exist between a set of (perhaps) viewport coordinates.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    Use some spatial mapping. For example quad tree for 2d. Or buckets in grid. There are more options of course.
     
    Ardenian likes this.