2DColliderGen - Perspective-based 2D Collider creation for 3D Objects

2DColliderGen

I’ve always been more of a 2D gamer and never really paid attention to Unity for that reason. A few years later it turns out that 2D is now natively supported. It was a little disappointing to discover that 3D and 2D use separate physics systems, so the goal for my very first script was to have 3D and 2D objects interact.

This is still very much a work in progress! Comments, suggestions, bug reports, optimization points, and anything else would be of tremendous help. I started using Unity about a week ago so expect bugs. The basic functionality is already there but I would like to refine this to be more than a little toy. Thanks!

Screenshots
Scene Setup: Just a 2D floor and ball in a 3D room.
The Box: Create a random 3D object that will serve as an obstacle.
Attach Script and Run: The ball now collides with the box.
Change Camera Projection: Using perspective? No problem.
Camera Camera Position/Angle: The collider is recalculated so that it “makes sense” from the new perspective.

Here’s how it was done:

  1. Pick a camera. This will serve as the origin of the projection.
  2. Pick an object. It must have a mesh.
  3. Create a 2D “Wall”, effectively a large 2D plane at the desired depth (Z).
  4. From the camera, draw rays to each vertex on the object and find where they intersect the wall.
  5. Take these points and find their Convex Hull. This finds the smallest polygon that contains all the points.
  6. Turn these points into the vertices for your 2D Collider.

Features/Settings:

  • Drag and drop 2D collider creation on any object with a mesh.
  • Call method “ColliderUpdate” to update colliders. This is only necessary for moving objects.
  • Use a custom camera or default to the Main Camera.
  • Ignore camera and use an orthographic perspective on the XY plane to speed up calculations.
  • Can recursively add script to all children at runtime.
  • Polygon/Edge Collider. Edge Collider option supports fully collinear shapes.
  • Specify a custom Z plane.
  • Require collider to intersect the object’s bounding box.
  • Ignore duplicate/nearby vertices to speed up calculations.
  • Rounding digits setting for float inaccuracies. It may help you get “tighter” colliders.

Planned Features:

  • Implement a better Convex Hull algorithm such as Chain/Graham. Currently using Gift Wrapping aka Jarvis march.
  • Create colliders in editor (need to research this)
  • Concave objects? (unlikely)
  • Option to use an object’s collider instead of its mesh

Known Issues:

  • Computationally heavy. You probably can’t put this in fixed update.
  • Cannot change between Edge/Polygon during runtime. This is currently intended.
  • If your wall is a straight line from the camera perspective then Polygon Collider will infinite loop. There is a breaker statement to prevent this but you still won’t get a collider. Use Edge Collider to support straight lines and employ a cutoff distance to ease calculations.

Updates:
2015.02.09 v1.00: First working version.
2015.02.10 v1.02: Minor cleanup. BoundingBox intersection and Material selection. CopyTo method to clone script.

1958273–126726–ColliderGen2D.cs (10.6 KB)

Wow! awesome! i was looking for a way to make 3d obj collide with 2d obj! keep up the nice work!

Interesting, but it there really a need? Can’t you just add 3d colliders on the 2d objects if you want it to collide with 3d stuff?