- Home /
Make objects collide only with other objects of same type
I realize that the title may be a bit confusing, but I just didn't know how else to phrase it.
Here is the situation: I am developing a multiplayer game where every player controls a set of objects. These objects can collide with one another and are not supposed to intersect (basically like ordinary physics with a rigidbody). They can, however, intersect with and pass through objects that are being controlled by other players.
To put it in another way: Player A controls N objects that are labeled "A-Object1, A-Object2.... A-ObjectN" and player B controls a different set of M objects that are labeled "B-Object1, B-Object2... B-ObjectM". A-Objects treat other A-Objects like regular colliders (e.g. floor, walls, etc.), but treat B-Objects like thin air and vice versa.
In a real environment, this scenario would NOT be limited to just two players, but rather a theoretically infinite amount of players. On top of that, the server will be authoritative, meaning that it will have the last word regarding collisions and/or pass-throughs.
How would one go about implementing such a functionality?
Answer by NaXDy · Aug 07, 2015 at 11:36 AM
I think I may have found a solution.
What I'm doing now is using the effects of 'Physics.IgnoreCollision' in a way so that I check in 'OnCollisionEnter' if the object is from the same player. If not, I add it to the IgnoreCollision part.
It's a rather dirty solution in my opinion, but it does seem to work, even with infinitely many players.
Whoops! I always forget about IgnoreCollision! It's obviously not as CPU-friendly as a layer-based method (which ignores collisions before they happen) but darn if that's not a way to get what you asked for. I apologize for the misleading answer,
It's alright. As the collisions will probably be handled solely by the server, I'm not too worried about CPU anyway, so that should be fine. Your suggestion might prove useful for other projects though, so thanks anyways!
Answer by AlwaysSunny · Aug 06, 2015 at 05:21 PM
There is no trivial way to do this with an "infinite" amount of players. You would need to bypass Unity's PhysX / Box2D physics systems altogether.
It may be that you can import an unwrapped version of Box2D (or another 2D or 3D physics library) to Unity, and bypass any limitations Unity's wrappers have imposed upon that system - potentially, any matter-of-course finite independent layer limitation.
Other than that, it's time to start looking at hacky solutions, changing your expectations, or something like a "shard" concept, used by many MMOs, where only N players are active in a concurrent shard instance, where N is less than the layer limitation.