- Home /
Mesh Colliders as triggers not working... Work-arounds (raycast)?
Hey Gang, I am playing around with a new game concept and need a bit of help getting it off the ground.
My game has thousands of cut out pieces of geo. Some concave, some convex. Each piece of geo is white. Once the player passes over it (triggers it), color is turned on via a script. I have this working (as you can see in the video below), by attaching a sphere collider to each piece of geo.
Here is a little video of what I have so far using sphere colliders. https://youtu.be/tdETeN12ON8
The PROBLEM: While using sphere colliders mostly works, there are many times that it simply does not because of the odd shape of the mesh. Placing multiple sphere or cube colliders per each piece of mesh seems out of the question because of the amount of time that would take me. The easiest answer would be to make each piece of mesh a mesh collider with trigger on, but that no longer works in unity 5 because of the latest physics engine. At least, that is what I have read and testing seems to support it. i should probably also mention that all the mesh being used is planar. Perhaps the answer is raycasting, but I am rather unfamiliar with raycasting and have no clue if it would kill performance.
Any suggestions on how I should go about doing this? It will be on a massive scale, so I really want to hammer down a clean way of doing it and am open to any suggestions.
Thanks all!
-Eric
Have you tried using compound colliders? $$anonymous$$ultiple mesh colliders on the same concave object which are individually convex so they can be triggers.
I have looked into this option as well. But, as I understand it, it would take a lot of manual placement of colliders. Which I don't see being able to do considering the game will have literally hundreds of thousands of collider triggers.
For what it's worth, there is at least one asset on the asset store that automatically creates compound colliders.
I don't see the comment any more, but someone asked....
"Is your desired end result to have very precise detection when the player is overlapping the geometry?".
The answer would be no. Precise is a bit strong. I just want something sort of close. An example right now would be, I have a sphere collider trigger on a banana shaped geo, and that simply doesn't work. I am aware that I could go in by hand and create three colliders to get me a banana'ish shape, and use that. But doing that on this scale just isn't feasible with how many times I will have to do this.
-Eric
Answer by Key_Less · Sep 22, 2015 at 11:59 PM
This seems like a very fun problem to solve and I have a couple links here that I think may help you. Take a look at this and this.
I haven't done much research on this, but if I were to attempt this issue I feel like I would start with the Separating Axis Theorem (SAT). From there I would try to improve performance by giving the player a radius check where the SAT algorithm is only applied to geometry that is within a specified distance of the player, and only geometry that hasn't already been overlapped by the player.
Hopefully these can help get the gears turning upstairs, I'd be curious to know what you come up with!
It is a fun one! I think it'll be quite rewarding if i can figure it out. I'm just wrapping my head around these links. Pretty scare stuff for a novice programmer. If i am understanding the gist, basically, these work in the player finding the outer edge of the mesh? Which would be perfect.
Answer by Hronet · Sep 22, 2015 at 08:46 PM
So my understanding is that you're currently using alternative colliders because that way at least it "works" and in this case it seems to be sphere to sphere collision?
I think you're afraid of using polygon colliders because of the overhead with checking complex shapes for collision (which you're totally justified in being hesitant of.)
I believe your best bet would be to use binary space partitioning in conjunction with polygon (or rather, mesh) colliders. In layman's terms, the binary space partitioning would let the player and objects in the scene know which 'quadrant(s)' (although that's a bad term here because there will be more than 4 subsections) that object was in. Once in there it will only detect collision with objects also in that space. This system will make it so your objects won't even both checking collision with each other if they aren't even in the same neighborhood, and this won't even be an if check, it'd be very direct.
The difference is like foreach( var obj in AllObjectsInScene ) obj.CheckCollision( Player ); and foreach( var obj in Cell[Player.x][Player.y][Player.z] ) obj.CheckCollision( Player );
I don't know if Unity inherently supports it, but I think it would help if you were able to get it.
Answer by iovergard · Nov 06, 2016 at 10:12 PM
Hi there, I ran into a similar problem and I solved it by creating an editor component that can decompose a concave mesh into a set of convex meshes which can be used as triggers. You can see and use the code on github
I'm not sure if there's a better way to do this besides solid leaf bsp trees, but that's the one I knew and it seems to be working pretty well for me.