- Home /
How do you create complex collision geometry to match 2D sprites?
I'm making a 2D game where objects spawn on the right side of the screen and slide to the left side of the screen where they collide with the player. Most of the objects are simple shapes where rectangular collision boxes work well but some are large, oddly shaped rocks and I need to have the collision boxes be a little truer to the shape of the image on the sprite. How do you create more complex collision geometry?
The only way I can think to handle this is to make a mesh in Blender or a 3D modeling program, then import the mesh and assign it as collision geometry to the sprite. Does this sound right? I'm not very experienced with this stuff and modeling is not something I have a lot of experience with.
Thank!
Yeah that's one way for sure - things like 2D Toolkit have that built in. You could build a unity tool to describe your objects shape - but it's a decent amount of work to make it nice graphically.
Answer by sparkzbarca · Mar 24, 2013 at 02:15 AM
you can also just use a compound collider.
baiscally you make an empty game object and you make it a child of the rock and add a collider
make another empty game object. make it a child. make a collider
you can then have multiple shapes for one collider. A compound colilder. it's built in supported.
So you can use a large square for the center area and smaller squares and spheres around the edges that togehter conform cloesly to the shape.
all your doing then is saying I want 1 large square + 2 smaller squares + 2 spheres + capsule or whatever I want all these to be a collider of the rock and anytime any of these are hit just treat it as if the rock was hit.
I thought I'd post some more information on the methods I found in case anybody's having trouble with this like I was.
The compound collider method sparkzbarca described. Create several colliders to replicate the shape of your sprite.
Use the 2D toolkit to click and drag points to create a polygon collider to match your sprite: http://www.unikronsoftware.com/2dtoolkit/doc/tutorial/creating_polygon_colliders.html
Create a mesh in a 3D modeling program to use as a collider for your 2D sprite. http://www.rocket5studios.com/tutorials/creating-2d-games-with-unity3d-part-5-complex-collision/
If you don't have the 2D toolkit or a program like Blender or $$anonymous$$aya, it looks like the compound collider method is the way to go. The 2D toolkit method is by far the cleanest, easiest way if you have it.
You guys seem to go with compound colliders in 2D without any trouble. Can you please try to recreate my scene and see if I'm doing anything wrong? I posted a question here: http://answers.unity3d.com/questions/652188/compound-colliders-not-working-in-2d-unity.html
Scene is as simple as it can get, but for some reason, I just can't make a compound collider work for 2D.
Thank you very much!
Answer by Asvaronn · Apr 29, 2015 at 09:52 AM
By now (probably added in Unity 5?) you can create a Polygon Collider 2D in Unity which does the same as the 2D toolkit polygon collider.