Is there a way to filter some colliders out?
I have many objects with box colliders attached to them, each of arbitrary size and position, like on the picture. If i get all the colliders those blue collider collides with, i will get a list that contains all green and red colliders. I want to use blue trigger collider to only store collisions with green colliders. Difference between red and green colliders is that red ones only have a single point where they touch the blue one, or a single line in 3d space.
Is this possible to somehow exclude the red colliders from the list?
Any help is highly appreciated!
Answer by BOB_KSE · Dec 27, 2017 at 09:52 AM
Its better if you use raycasting instead of boxcolliders.
Its difficult to check where the box collider collision is occuring from and which collider are the ones we need.
Raycasting is better because you can put rays on places where you want to recieve data from, In your case every point except the corners.
@BOB_$$anonymous$$SE thanks, but there is a problem with raycasting. it has to be some tricky algorithm to deter$$anonymous$$e the points i should perform raycasting from and to. here is an explanation intuitive algorithm 1: raycast from the center of each of the other colliders towards the normal of the other collider side
https://image.ibb.co/fNLAbb/colliders1.png
in this case raycasting from purple collider will not detect blue collider and purple collider will be filtered out as well.
intuitive algorithm 2: perform multiple raycasts from the blue collider with a step less then the $$anonymous$$imal other size of the collider, which for simplicity is a cube with a 1 unit size.
https://image.ibb.co/iSf$$anonymous$$hG/colliders2.png
in this case everything seems to work well, but if a blue collider is a cube size of 30 units, it will require to perform 5400 raycasts (30*30*6 sides), which is also a no go, as it has to be done every frame
edited for better readability
Sorry, but the images you posted in the comment failed to load. so, I can't fully understand what you are trying to say. your rays need to be spaced a little smaller than your 1 unit size block. so that all touching blocks are sensed.
I suggest you watch the first two videos on this playlist. he uses something called as skin width
to project the rays from which can be useful in your case. he is making a 2D platformer though.
yep, sorry, i tried to upload new images multiple times, but unfortunately it doesn't work for me anymore too, nor by editing post, nor in comments, (i tried in firefox & chrome) here is direct links to images then
case 1
https://image.ibb.co/fNLAbb/colliders1.png
case 2
Answer by BenSan · Dec 27, 2017 at 12:40 PM
You can use several approaches:
Disable physics between red and blue objects entirely (by layer) ->https://docs.unity3d.com/Manual/LayerBasedCollision.html
You can simply check the collision that you are subscribing to if the tag or layer (or even name, not recommended) is the one you want https://answers.unity.com/questions/261556/how-to-tell-if-my-character-hit-a-collider-of-a-ce.html
You can place the red elements with a very small gap, like 0.0001 and make sure they don't collide
If you are creating the parts by code you can simply add them to a collection and use that instead (bit out of the scope of this answer)
im building a ship from all those boxes, all the boxes except the blue one exist, and i dont know which one is red, green or white, i have to deter$$anonymous$$e it, that's the point. They have to be on the same layer, the only object that can be in different layer is the blue one, as it is not built yet
As all the other collider object are a part of the same ship, that belongs to the same player, there is the same issue with the second approach.
I dont quite get the 3rd option, i need o deter$$anonymous$$e neighbor blocks somehow anyway, or the rest of game logic will break
If you are creating the ship procedurally there is no need to get the other parts via collision as this creates an extra overhead anyway. Unless you want to add every part that collides with your ship $$anonymous$$atamari style. When you generate the other parts just add them to the collection wherever you are storing it so you always have a reference.
I don't know what $$anonymous$$atamary style is, but i create ships manually at this point, and they are supposed to be built by players in game, block by block. The reason why i want to filter colliders is to prevent ship design like this:
https://image.ibb.co/f3ByEw/colliders3.png
Right now i use additional compound collider to do the filtering, but it feels like abo$$anonymous$$ation in the code and requires additional work with each prefab. Looks like this: