Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

[Solved] Test BoxCollider for collision before placing it

Discussion in 'Physics' started by Magister, Jun 8, 2018.

  1. Magister

    Magister

    Joined:
    Mar 29, 2013
    Posts:
    22
    I'm procedurally generating levels and I want to test if placing a BoxCollider would collide with other rigidbodies before I place it in order to prevent overlapping objects that cause the physics to freak out and objects to shoot off into space. I found Physics.OverlapBox which I think should do the trick but I'm having a very difficult time figuring out how to pass the equivalent of my BoxCollider to it. The BoxColliders can be at any position, scale, and rotation on top of having any center and size so I don't know how to calculate OverlapBox's center, halfExtents, and orientation from that. I also can't actually see what area the OverlapBox ends up covering so it's very difficult to try and debug my math.
     
  2. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    45
    Can you share the part of code where you generating BoxCollider ? So i can help you with getting info from it for OverlapBox.
     
  3. Magister

    Magister

    Joined:
    Mar 29, 2013
    Posts:
    22
    I ended up solving my problem without using Physics.OverlapBox. Instead I moved my code into FixedUpdate and set my BoxColliders to be triggers. I added a OnTriggerEnter that would record the 'collision' happening, and then in the next FixedUpdate or Update I check that record to see if any triggers were entered. If not I switch all the BoxColliders back from triggers to normal colliders. If a trigger was entered then I remove all those BoxColliders and associated objects instead.

    It was a little weird at first to structure my code like this, but not that hard once I had the idea. Also, from a little bit of testing it seems that using Physics.OverlapBox is significantly slower then using triggers. I don't know for certain that this is always the case but it was in this scenario.
     
    bakir-omarov likes this.