- Home /
How to use 2D physics for 3D models
Hello. I'm making a game that uses 3D models, but to increase performance I planned on using 2D physics. However, when I try to add a BoxCollider 2D it doesn't show up in the scene, and I get the error "The collider did not create a collision shape as it failed verification. This could be because it was deemed too small or its vertices were too close. Vertices can also become close under certain rotations."
I tried making my model bigger, but that didn't solve the issue. Do I have to use 3D physics for 3D objects?
[edit] I've found a really hacky work around. If use 3DSMax to render a png of the model, then attach a sprite-render to the object, create the 2d physics polygon, then delete the sprite-render I get the desired results. Surely there is a better way of doing this?
[edit2] I found the reason this is happening. When applying a BoxCollder2D to a 3D object, unity seems to take a flattened image of the object from the Z plane (that is to say, with Y being vertical and X being horizontal) but I was rotating my objects by 90 degrees, making that plate on which it's trying to make the collision box flat:
http://i.imgur.com/AIg6v5q.png (worked on the left, error on the right)
I'm still not sure how to fix this, but at least I have something to work from now. I guess I could open the models in 3D studio and rotate them there, then export them again.
"to increase performance", what exactly is your game doing that it requires this?
there are better ways to increase performance than trying to hack a solution, identify where the performance leak is first and address that.
The problem is the number of simulated bodies. With the 2D physics engine I can get about 2000 without slowdown, but with the 3D physics engine it was starting to slowdown around 1000.
I believe your hacky way is the best approach. The 2D physics system was designed to create colliders based on the sprite mesh. You give it a 3D mesh to start with and it gets confused.
You can probably make an editor script that takes a 3D model, grabs the vertices, calculates the furthest out, flattens it to a 2D mesh, then hands it to Unity to create the polygon collider from.
Answer by Kiwasi · Oct 10, 2014 at 09:51 AM
You should be able to add a 3D object as a child of a RigidBody2D. At worst case you can attach a script to your model that makes it follow the RigidBody2D. You will probably have to build custom colliders to get it to match your model.
In terms of performance I don't think you will end up gaining much, if anything. I'm only listing this as a solution because I can come up with a couple of cool concepts using a 2D game with 3D graphics, or vice versa. But you need to build it in as a designed mechanic, not a performance boost.
2D colliders are generally somewhat faster than 3D colliders, so it's a reasonable optimization if the physics are all going to be 2D anyway. (Just don't expect it to be massively faster.)
Answer by Josh Naylor · Oct 10, 2014 at 09:35 AM
Edit: Apparently so.
Hmm then I guess I was mistaken. I thought there was no difference between 2D and 3D projects, that sprites were simply quads. Just that the 2D and 3D physics couldn't interact. Which would have been ok, because I didn't need them to. I didn't want ANY 3D physics.
You can certainly add 2D physics to 3D objects; all objects are 3D objects after all. The collider you add to an object doesn't have to be related to the mesh in any way and is a totally separate component.
You are close. There is not much difference. Sprites are not quads, they are custom meshes built to reduce the transparent area on sprites. This helps reduce overdraw. But the idea is essentially the same.
Well, sprites are usually quads. If you have Pro, you have the option of making sprites be a tight mesh, or a quad. If you have Free, they are always quads.
@Eric5h5 Are you sure? I thought the mesh optimisation was available on free. I'll play with it later.
Answer by Eric5h5 · Oct 10, 2014 at 09:53 AM
Sounds like the scale of the model is way off. The box collider should ideally be about 1 unit in size.
The model I was trying to add it to was about 2x1x1 (but let me double check to be sure)
But you're saying I should be able to apply the BoxCollider2D directly to a 3D mesh?
Also you can always change the size of the box collider in the inspector.
Technically, you add a box collider component to a GameObject. You can also add mesh filter and mesh renderer components to a GameObject. So you don't really add a box collider to a mesh.
The problem was/is if you rotate the mesh by 90 degrees, unity is building a collider with a size of 0 on the X axis. I think I may need to rotate all of my models then re export them.
Thank you for the help by the way!
Your answer
Follow this Question
Related Questions
Objects stuck dragging across floor 1 Answer
Using a sprite sheet in a 3d enviroment? 0 Answers
Physics - strange jumps. 2 Answers
Is it required to use 3D physics when a 2D game uses the z axis? 1 Answer
Should 2.5D use 3D or 2D settings ? 3 Answers