Destroy object on collision or check position

In the scenario where multiple rigidBodys moving due to addForce goes out of view (say, top of view) and is no longer needed then what technique should be prefered?

  1. Destroying the gameObject by detecting collision
  • by creating a collider object and destroying the rigidBody when it collides with it
  1. Destroying the gameObject by first checking it’s rigidBody position in Update or FixedUpdate

Premature optimization or not, I’d still like to know
Thanks,

if you looking for optimization i would use object pool and avoid instantiate\destroy.
for remove unused objects of the screen i would use collider set as trigger so you don’t calculate
collision physics and you don’t need to check things every frame (or every X frames)

learn about object pooling:

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

Both are perfectly valid methods. What is preferred is subjective and depends entirely on what works best for you and your game.

The first scenario has the issue that it will be calculating collision for all objects (which have colliders) which might not be the objects you need to destroy. This can easily be solved by putting the objects in the correct layer and modifying the collision matrix so the destroyer can only detect collision from the correct rigidbodies.

The second scenario is fine, you sort of get greater control of what specific objects will be checked, but could have issues if the objects are big and then might still be in view when their root position is not. There is also some complicated calculations to do if the camera is a perspective camera (the farther away the object is the higher up can it go before it is out of view). You could also get some issues if the camera can move around. All of these issues can be easily solved. Example: Having the camera in Orthographic mode (not applicable for all games), or not moving the camera at all, or never moving the objects in depth, or if you really want a perspective camera which moves around and the object should still be destroyed when out of view you could use OnBecameInvisible callback instead of checking positions.