Search Unity

Bug Why is my enemy spawning inside tilemap collider?

Discussion in '2D' started by jleven22, Mar 2, 2023.

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421


    The red diamond represents the spawn position of an enemy, which is 1x1 (same as square tile).

    This enemy, among others this close to the collider, is spawning not on the red diamond, but inside of the the tilemap collider just next to it.

    Here is my instantiation set up on the red diamond gizmo:

    Code (CSharp):
    1. GameObject spawnedEnemy = Instantiate(enemies[randomEnemy], transform.position, transform.rotation);
    2.  
    Any ideas why the enemy isn't spawning in the right position? Perhaps this is some collider issue I'm not aware of? I'll note it's only when an enemy is being spawned within 1 unit distance like the image above.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    There's no way to remotely debug this for you. All we can see is an instantitate call so it'll be at that transform position.

    You don't mention what you're doing to pick this transform.position.

    The image isn't clear here either, can barely see the collider gizmo but from the looks of it, it's an outline (edges) with a radius so likely using a CompositeCollider2D with the tilemap. If you're performing a query to check to see if an area is inside a tile, you won't get anything because there isn't a collider inside the tiles, it's only an outline (edges). Again, all a guess because you didn't say this is what you're doing so maybe wrong.
     
  3. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Sorry for the lack of detail. You assumed correctly, it's a composite collider on the tilemap.

    My concern is the fact that it is all happening in the instantiate call - as long as an enemy (size 1 unit) is within 1 unit of the tilemap composite collider, it sometimes spawns inside the tilemap colllider.

    I'm not even sure how to debug it honestly.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Seems you missed what I said which is you didn't say how you're checking whether it's "inside" the tilemap collider. A physic query? Again, there is no "inside" from the physics pov, it's just outlines (edges).
     
  5. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Oh well I'm not checking in code. I'm seeing the enemy visually inside the edges. And once it's in there it's trapped.

    I'll try and get a screenshot to share.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    No need for a screenshot. As I said, outlines are just that outines (edges) so I'm not sure what else you expect here.
     
  7. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Er... I'm hoping for help? If you don't want to it's okay, someone else can maybe try.

    I'm wondering why enemies are spawning one unit over. It's like the fact that they're within 1 unit of the composite collider edge allows them to move inside the edge rather than stay on the outside.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    I have no information on exactly what you're doing beyond you spawning objects.

    You're not checking what? You're not checking if you're spawning over an edge? Not checking if you're spawning "inside" a tilemap collider outline? If not then how do you expect to not be "inside"? I don't know what physics components you're using here if any on these things you're spawning. For all I know they have a collider on them and after you spawn, the solver moves them out of overlap with the edges. You'd obviously be able to easily debug this by looking at callbacks for it.

    So please, if you want help, you need to proactively explain what you're doing here in some detail beyond just saying you're spawning so it's not a guessing game. :)
     
  9. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    I don't know what to say, but it appears this bug resolved itself.