Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question BoxColliders/OverlapBox.OverlapSphere not working as expected?

Discussion in 'Scripting' started by ferzle, Jun 20, 2023.

  1. ferzle

    ferzle

    Joined:
    Jun 20, 2023
    Posts:
    2
    I am new to Unity, but an experienced programmer. I have been trying to debug some code for over a week now. I have created a maze of blocks (3D blocks, 2D maze) that water and lava can flow through. I am using water and lava prefabs that I grow and shrink as they go through the maze--so each cell of the maze is 1x1, and as water displaces lava, the water grows as the lava shrinks. When I resize, the objects, I am also resizing the BoxCollider of the objects. To determine if I can put a new block somewhere, I have tried using OverlapBox and OverlapSphere, and they both have the some problem: Every once in a while they do not collide with an object that I am certain is there, resulting in a second (or 3rd or 4th) object spawning in the same spot. I can provide more details later, but for now I am wondering if I am totally misunderstanding how Unity does collisions. Since I am modifying the BoxCollider programmatically, I am wondering if this could be the problem. When I print things, everything looks like it is in the proper space and is the proper size, and there is definitely an object there to hit, but it is not returned. I tried Physics.SyncTransforms(); in several spots since I saw that in another post, but that doesn't help. Any guesses?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    It really should "Just Work(tm)" if you're doing what you describe.

    Since you're on a grid, you could modify your code to detect the collision via coordinates that you track yourself in a 2D grid.

    If that is ultimately not an acceptable solution, it could still be a solution to help you identify what is wrong: if you do an overlap at cell X,Y, go check your grid and if something actually IS there at that cell, do a Debug.Break() and print all the stuff out relevant to where the error happened.

    This leaves the editor paused with your entire scene set up and you can go digging and see what is truly there.
     
  3. ferzle

    ferzle

    Joined:
    Jun 20, 2023
    Posts:
    2
    This is what we started planning today. The idea is simple enough that we can do the calculations without using Unity's collisions, etc. It also goes along with the concept of separating the model from the view which I like. I am still pretty baffled as top why what we are doing is not working as expected. I am pretty certain the calculations we are doing are correct (i.e. things are where I expect them to be), so I cannot for the life of me figure out why the collisions aren't being detected.