- Home /
what is the best way to detect this collision?
I need a way to properly detect if the red dots box is colliding or not, in my script I have a random room generator and when it instaciates, I check wich direction is open and not with GetComponent().IsTouchingLayers() but it doesn't work was i would like, I think the problem is as the documentaction says its to slow to use as a I want, Im actually instacianting tilemaps prefabs with composite collider using a room generator gameobject with 4 childs colliders set to trigger in each direction.
the problem for example is:
instaciate(randomcave, position, rotation); //works fine
bool South = transform.Find("South").GetComponent<Collider2D>().IsTouchingLayers(); //returns false
but it only returns false after the instaciate if i keep cheking and printing it in the update it returns true
Answer by Hannibal_Leo · Sep 24, 2019 at 02:00 AM
There are so many solutions to your problem. An easy fix would be to use Collider2D.IsTouching you know of all 4 "red dot boxes" so you can check against them. You could also use the colliders Bounds as all your tiles are rectangular. But the best way to work with a dungeon generator is to create a Grid, in code this would be represented as a 2D-Array where each tile is stored and you can declare your "red dot boxes" in the tilegrid and react on them, if you want to place a tile on one of the "red dot box"-tiles.
It depends on what you want to archive.
Answer by Naggil · Sep 25, 2019 at 06:01 PM
well I find out a way to work around the problem using invoke, it makes the cave generation a little slow but as everythink is working now i don't need to worry, the real problem was that is.touching only take place after the physics update i just made sure that it was enough time to it update now I only need to put laziness aside and finish off all the empty rooms that i was using just to code using tilemaps prefabs workout really well for me, it could be a real problem to use a script grid and spawn sprites one by one as most of my walls are just a border it would be a real pain in the ass also as im using the 2d extras assets it is really easy to instaciate objects in the tilemap (as all those stones around the map)
is there even a down side using instaciated tilemaps prefabs?
Your answer
Follow this Question
Related Questions
2D collision won't work? 2 Answers
Gameobject not detecting collison from other Box Collider 2D [SOLVED?] 2 Answers
2D - Two objects, each having two trigger colliders - interacting 0 Answers
Problem with method Collider2D.isTouchingLayers() 4 Answers
How come people prefer to use rays for 2d collision instead of edge colliders? 1 Answer