I'm working on a 2D game that lets the user move physics enabled objects directly (using the mouse or touches). These GameObjects have colliders and rigidbody attached. For now, we're using boxes, but we plan on having more complex shapes, so we may need to use mesh colliders.
I need to prevent the user from dragging a shape into another one, while still being able to stick them one against the other. I tried using Rigidbody.MovePosition, directly changing the transform position and using AddForce, but all these methods cause varying levels of penetration between objects.
I had excellent results with SweepTest, only moving the object if SweepTest returned false, but SweepTest only works with primitive colliders, NOT with mesh colliders (even when enabling convex mode).
I've considered using MovePosition, then check if the collider fires the OnCollisionEnter callback, and then move back the object if it does, but the callback executes too late, it isn't called as soon as two objects collide, but only at the next FixedUpdate. It could be managed, but would be very cumbersome to work with.
It seems like the only reasonable solution is to combine primitive colliders to form complex shapes, but I'd like to make sure if there isn't another solution, if anyone would like to share it.
It would be great if there was some form of direct collision testing. Something like TestCollision(colliderA, colliderB), that would return true or false, and work with mesh colliders. A bit like SweepTest, but without the sweeping part.
Any pointer is super appreciated, Thanks!
Remember that mesh colliders only interact with primitive colliders, not other mesh colliders! (unless you mark them as convex). Could that be your problem?
Yes, they're all set as convex. They actually do collide. The problem arises when a user tries to forcefully drag one object into another, visually, they'll overlap. As soon as there is no more interaction from the user, the objects get out of each other, but it doesn't look too good, and sometimes, it creates explosive results, as the physics engine adds forces to try and separate the objects.
Well, if you use MovePosition along with OnCollision callbacks, you could set something up where it just doesn't move any further in that direction if it detects that it has run into something. A little hacky I know, but it might work for you.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
3 People are following this question.
Crawling around the terrain
Giant colliders restricted to a set size?
Glitchy Convex Mesh Collider
Terrain Detail Mesh Collisions
Mesh and Sphere Collider Not Registering