3

So there are lots of squares (about 200-300) in my scene. They are moving a little, and they need not to cover each other. It's pretty hard for a computer to add Rigidbody2D to them. I tried to add BoxCollider2D and Mesh Colliders on each object and code OnCollisionEnter2D in the script, but it just doesn't work. Convex MeshColliders don't work (at 2D, I suppose. why?). So how can I deal with it without using Rigidbody? Is it a wrong way to use Collider2D?

EDIT:

First of all, I'm sorry for my bad english.

Secondary I want to thank all of you for your answers. These are great references and I'll spend much time to go deeper in that. I've found the answer, it's quadtree. There's a good example/tutorial in this page

  • 1
    When there's a lot of entities interacting and if you only want to know for collisions, you could implement your own algorithm, like [quadtrees](https://en.wikipedia.org/wiki/Quadtree). There's a good example/tutorial [in this page](http://gamedevelopment.tutsplus.com/tutorials/quick-tip-use-quadtrees-to-detect-likely-collisions-in-2d-space--gamedev-374), pd. I don't know if *OnCollisionEnter2D* use this approach – Alex S. Diaz Dec 15 '15 at 23:38
  • Oh, thanks a lot! It's exactly what i looked for:) – Gleb Zakharov Dec 16 '15 at 11:36

2 Answers2

1

"It's pretty hard for a computer to add Rigidbody2D to them." Do you mean it has performance cost?

Rigidbodies are used for detecting collisions. You don't need both of the colliding sides to have a rigidbody. For example if you have 100 squares in your scene as obstacles, you can have collisions if your player object (e.g. a circle as a ball) has Rigidbody component. In this example, the ball needs CircleCollider2D+Rigidbody2D, the obstacles need only BoxCollider2D.

Sean Ed-Man
  • 378
  • 4
  • 8
  • Yeah, scene is loaded really slowly if I add just about 80 `Rigidbody2D`. The game needn't support physics2D. – Gleb Zakharov Dec 17 '15 at 16:55
  • if only one side of the colliding parts, the physics works too. And for 2D, all your colliders must have '2D' post-fix. Because the 2D physics engine of Unity is Box2D and 3D physics engine is PhysX, so you can't mix the effects (e.g. a Rigidbody2D won't collide with a SphereCollider). – Sean Ed-Man Dec 18 '15 at 19:20
0

You should use box colliders instead of mesh colliders. By this way, you can gain more performance. And your collider's "Is Trigger" property should be checked. Rigidbody component's "is kinematic" property should be checked too. By this way, all gameobjects in your scene dedect collision each other.

OnTriggerEnter2D() function can help you catch collision detections between gameobjects in your scene.

Halil Cosgun
  • 427
  • 3
  • 10
  • 24