- Home /
Concave Trigger on Mesh
In my game, I have set up splines that the player can grind along as rails. I have created a script that takes the data from each of the splines and draws a mesh collider based on their shape that acts as a trigger to mount the rail.
This is the best option for me, as it allows the players to create their own rails and saves me and my team time placing the rails without spending ages placing primitive mesh colliders along the spline.
The only problem I have with this system is that Unity doesn't allow me to have concave colliders as triggers, which means that I can only have convex colliders encompassing all of my rails. This is ok for things like this:
But if I want complex and curvy rail systems ...it comes out like this:
Obviously, there is no way that I can use this in my game.
Is there any way that I can get a concave mesh to act as a trigger -that doesn't involve me spending money on an asset, or using an older version of Unity?
If not, are there any workarounds?
Thanks in advance.
Answer by JRonde · Oct 19, 2017 at 09:29 AM
I had a similair problem in a rail design game we're developing. One of the features is that it should detect rail assets clashing a buffer zone of the centreline of the rail track. I created a script that extrudes a profile along the track and creates a single mesh for the buffer. I couldn't use this mesh however because of the same reasons you mention. Unity doesn't support a non convex mesh to act as a trigger. Quite a limitation if you ask me.
I didn't see any other option then just rewrite my mesh creation script to create a mesh foreach section of the track. Perhaps you can do the same. I still prefer the buffer to be a single mesh however. Makes everthing much more simple.
In the end, I used the spline-generated geometry as a collider, and then just checked to see if the player had hit a collider tagged "rail" using OnCollision(). Not a very clean solution by any stretch, but with some simple use of physics layers I was able to make it work with little hassle.
It's a shame that unity doesn't permit you the freedom with complex collision shapes that other engines do. I get that there is an obvious performance hit, but I imagine optimizing this example in particular would have been extremely simple for me if I'd been given the ability to use them as triggers.
Thanks for your answer anyway! Hopefully these workarounds can be of use to other people facing the same problems. :)
Your answer
Follow this Question
Related Questions
How to fix Unity 5 Mesh Collider Convex Problems? 1 Answer
Trouble With Imported Model Collider 0 Answers
Bucket with apples on a terrain (Sphere Colliders + Mesh Collider + Terrain) 0 Answers
Collider Mesh Is Not Tight Enough 0 Answers
Unity 5 Mesh Triggers - OnTriggerEnter / OnTriggerExit called repeatedly 1 Answer