0
\$\begingroup\$

I have a series of models from Blender I'm importing to Unity as .blend files and I need mesh colliders applied to each one.

What I'm finding is that with any model that is constructed of multiple primitives, applying the mesh collider to the object is unsuccessful. The mesh collider can't find the mesh. Unity seems to treat each shape inside the model as a separate object. I've even noticed that the Unity editor will allow me to select a single piece of a model and drag it away if I'm not careful. This is unexpected behavior and somewhat alarming.

I've discovered that if I apply a separate mesh collider to each individual piece inside the model, the colliders will find the sub-meshes and the collision detection will work. My primary concern at the moment is that this is monotonous. For example, if I have the following model of a road intersection, then I'll need to individually select each of the seven pieces and create a mesh collider for each one.

example model

How can I apply a mesh collider to the entire model in one move?

UPDATE:

Based on some feedback and some thoughts on my own, I'm redesigning the road pieces to simply be solid road on the bottom with the sidewalks on top. This way, I'll only need one collider for the bottom, for now (if I don't care about testing collisions with the sidewalk edges yet).

new street

I'm hesitant to merge the meshes into a single mesh at this time, though, since I'm potentially not done modelling street pieces yet. This also doesn't address the long-term question of what to do in the final version of the game.

Unity primitives have automatic collision detection, but simulating hills this way and still having the pieces meet seamlessly is difficult. They also won't do curved surfaces as far as I know.

I'd rather not use box colliders, since that will involve a lot of painstaking zooming in and tweaking of tiny values to get them perfect. It also seems that box colliders will only rotate with a model and not to a model that's already sloped.

What I'm trying to avoid is spending almost as much time messing around with the colliders as I did modelling these bits in the first place.

Sticking with the mesh colliders is looking like the best option for now, since most of these shapes are simple, 6-sided objects and it's only a couple of clicks to add a mesh collider to each shape. It sounds like if I do it this way, it might be smarter to keep the meshes un-merged, since I would then be using a mesh collider on a concave object.

\$\endgroup\$
  • \$\begingroup\$ Note that the road piece you've shown can be well represented by convex colliders or even primitives (a box in each corner plus one for the base). If you were to make a single mesh of this and wrap it with a single mesh collider, it would need to be concave, which is much more expensive to process physics interactions on. So although it may be tedious, it might ultimately be better for your game's performance and stability to make these colliders piece by piece. \$\endgroup\$ – DMGregory Mar 13 '18 at 11:39
  • \$\begingroup\$ @DMGregory, the piece-by-piece method is what I'm currently doing, but with mesh colliders. Most of the road sections are sloped or angled and the problem with box colliders is that they can't be rotated (as far as I can tell). The rough draft of the city was done with primitives, but they're very difficult to get lined up smoothly once they've been rotated a few degrees, so I'm taking this modular approach. I'm not sure what you mean by a "single mesh", but I'm still fairly new to modelling. \$\endgroup\$ – Nightmare Games Mar 13 '18 at 16:39
  • \$\begingroup\$ Those individual pieces you can move around are single meshes. The larger tile object you've called a "model" is just a group containing multiple child mesh objects as exported by Blender. You can add your own child objects with box colliders to rotate them any way you need. \$\endgroup\$ – DMGregory Mar 13 '18 at 16:52
1
\$\begingroup\$

The best way to do this would be to combine all meshes in blender and make a single mesh, preferably with lower quality, and use that. ButI wouldn't really recommend that for performance reasons, especially for something as simple as a road.

However it is possible to have a box collider and have it rotated. Assuming that you create a road mesh that is flat, create a box collider and attach it to the road. Now if you place your road mesh on a scene (moving it around, rotating or scaling it) the box collider does the same. This is what I'd recommend.

\$\endgroup\$
  • \$\begingroup\$ That sounds like it would work. However, I found that trying to rotate the roads to simulate hills led to a lot of bumps, seams, and gaps in the road where the edges meet that were difficult to fix and made the driving unpleasant. I've been mitigating that by using modular road models in pre-bent shapes that would meet seamlessly, which is working well. It seems I may have to choose between two evils, then. \$\endgroup\$ – Nightmare Games Mar 13 '18 at 19:59
  • \$\begingroup\$ @NightmareGames perhaps you could try adding multiple colliders, like one box and two spheres, and move them around depending on your mesh? \$\endgroup\$ – TomTsagk Mar 14 '18 at 14:19
  • \$\begingroup\$ I don't quite understand that last recommendation. Are you suggesting creating pre-defined collider configurations outside the road objects and copying and pasting them into the appropriate locations? \$\endgroup\$ – Nightmare Games Mar 15 '18 at 6:23
  • \$\begingroup\$ @NightmareGames no, I mean to take a single road mesh (one that has bumps or is curvy as you said) and add 2-3 colliders to it, maybe 3 box colliders or 1 box and 1 sphere collider, that they roughly take the shape of the deformed road mesh. \$\endgroup\$ – TomTsagk Mar 15 '18 at 9:59
  • \$\begingroup\$ Okay I see what you mean. That sounds like it might be more work than simply making all the roads with Unity primitives, though. It also sounds like it would create a very bumpy ride for the player at best. \$\endgroup\$ – Nightmare Games Mar 16 '18 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.