Search Unity

Physics.OverlapSphere not detecting Instantiated Objects

Discussion in 'Scripting' started by Enduriel, May 8, 2019.

  1. Enduriel

    Enduriel

    Joined:
    Jun 21, 2018
    Posts:
    3
    I'm trying to make a grid (in the future hopefully also other shapes) of Hexes in which all the Hexes find their neighbors by creating a Physics.OverlapSphere and adding all Gameobjects that are within a certain radius (in my case 2 as the radius of the circumcircle of the Hexes is sqrt(3). I instantiate all the Hexes inside of a Hexgrid GameObject
    Code (CSharp):
    1.     private void GenerateMap()
    2.     {
    3.         for (int x = 0; x < size; x++)
    4.         {
    5.             Vector3 pos = gameObject.transform.position;
    6.             if (x % 2 != 0)
    7.                 pos.z -= 1.5f;
    8.             pos.x = gameObject.transform.position.x + 2.25f * x;
    9.  
    10.             for (int i = (x % 2); i < size * 2; i += 2)
    11.             {
    12.                 GameObject newHex = Instantiate(hexPrefab);
    13.                 pos.z -= 3;
    14.                 newHex.transform.position = pos;
    15.                 newHex.name = x + "_" + i;
    16.                 pos = newHex.transform.position;
    17.                 board[x, i] = newHex;
    18.                 hexes.Add(newHex.GetComponent<Hex>());
    19.                 newHex.transform.parent = transform;
    20.             }
    21.         }
    22.     }
    and then call a GiveNeighbors function to find all the neighbors of a Hex
    Code (CSharp):
    1.     private void GiveNeighbors(GameObject hexObject)
    2.     {
    3.         Collider[] colliderNeighbors = Physics.OverlapSphere(hexObject.transform.position, 2);
    4.         //hexObject.GetComponent<Hex>().makeCircle(2);
    5.         //print(colliderNeighbors.Length + " | " + hexObject.name + " | " + hexObject.GetComponent<Collider>().transform.position);
    6.  
    7.         foreach (Collider collider in colliderNeighbors)
    8.         {
    9.             if (collider.gameObject.GetComponents<Hex>() != null && collider.gameObject != hexObject)
    10.             {
    11.                 hexObject.GetComponent<Hex>().neighbors.Add(collider.gameObject.GetComponent<Hex>());
    12.             }
    13.         }
    14.     }
    hexPrefab:
    upload_2019-5-8_13-13-24.png
    However, Physics.OverlapSphere does return the colliders of any instantiated objects (including a Pawn object that should still be recognized) but it does return the colliders of objects that were placed on the scene before I run the game:
    upload_2019-5-8_13-18-10.png
    The List of neighbors for that Hex contains ONLY the hex that I placed there beforehand that you can see hovering slightly above.

    I have no idea how or why this has been happening and after about 4 hours of scouring the internet I've given up and turned to here for any help.

    (I know I could do this differently, but using a Physics.OverlapSphere lets me be very flexible the future shape of my map which is why I'm really trying to make it work like this)

    Any help would be VERY much appreciated.

    EDIT: Added code with tags

    EDIT 2: Final Structure for my GameObjects

    upload_2019-5-8_18-57-46.png
     

    Attached Files:

    Last edited: May 8, 2019
  2. Enduriel

    Enduriel

    Joined:
    Jun 21, 2018
    Posts:
    3
    If anyone wants me to try something or even get the source code to try and fix this themselves, message me and I will send it to you because I'm stuck unable to do anything without this working properly.
     
  3. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Things to try:
    Massive overlap sphere, does it get anymore neighbours.
    Place a few manually do they detect each other.
    Do they all have colliders.
    Are rigidbodys needed to be picked up in an overlap shere check?
     
  4. Enduriel

    Enduriel

    Joined:
    Jun 21, 2018
    Posts:
    3
    Ok, got an... odd result trying a big sphere, some of the hexes are detecting ALL of the other 399 Hexes as neighbors (even though they shouldn't in terms of they are too far away) and others are still detecting nothing. After a couple of tries with resizing the OverlapSphere, I noticed that the once that detect all the Hexes that see neighbors should would be within range of the HexGrid GameObject (which Instantiates and then stores all the hexes in an array), even though it doesn't have a collider. Is it possible that the OverlapSphere can only detect the colliders of GameObjects that aren't children?

    upload_2019-5-9_16-21-41.png

    When my OverlapSphere is size 8, these Hexes all have 399 or 400 neighbors
    upload_2019-5-9_16-40-20.png
    As seen here
    upload_2019-5-9_16-24-14.png

    Edit: They do all have colliders
    Edit #2: Correction on the Hexes within range
    Edit #3: Overlapsphere size 6, these Hexes have 399 or 400 neighbors
    upload_2019-5-9_16-58-28.png
     

    Attached Files:

    Last edited: May 9, 2019
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Just throwing some theories out there.
    Are you instantiating and spherecasting all within the same frame?
    It may be that the physics engine need another tick before you can reliably spherecast against them.
    Try waiting a physics frame / fixed update after instantiating before spherecasting.
     
    Scrat1905 and lordconstant like this.
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    @ThermalFusion is probably correct in that it's due to instantiating and spherecasting in the same frame. You don't have to skip a frame, though, you just have to tell the physics engine to sync transforms, by calling Physics.SyncTransforms.
     
    Gialbo, Yadoyad and ThermalFusion like this.