I assume you want to optimize the physics. And that was the main reason of this question.
If your game isn't using any physics that depend on frame rate and uses physics only at some points in time defined by game logic - you can turn off physics calculations and simulate them when necessary Physics.Simulate. This will greatly improve the performance of a game that has physics components used in great quantities.
To improve the performance you might as well look at Physics Best Practices - use physics on separate layers, collision matrix will help. Make use of
LayerMasks. Use raycasts in an optimized way.
As per your question:
You can cache colliders and use them in components. Code is untested and might contain some typos. As well some of the colliders might not have `bounds.
public class Interactable2D : MonoBehaviour
[SerializeField] private Collider2D _collider2D;
protected virtual void Interact()
// Do something
public void Interact(Entity/Collider/Player entity)
private void Reset()
this._collider2D = this.GetComponent<Collider2D>();
OnCollisionEnter2D - is an event, a message to be totally correct, though, from experience Unity probably caches function reference. It would be unwise to use reflection to call this method or any similar methods like
It won't be invoked if you don't implement it. Calculations for
OverlapPoint will only include calculations necessary to check if point is in bounds.
My own suggestions
I would advise you to use Unity internal physics system instead of handling collision checks with
OverlapPoint in case you are going to use it the way I have shown in the script (without additional collision detection). - Because - game engines usually use OcTree(3D), QuadTree(2D) to handle collisions. They separate collision detection in chunks. Because comparing collisions of every
Rigidbody/2D with every
Collider/2D is O(n*m) (n - quantity of rigidbodies, m - quantity of colliders) - correct me if I am wrong. Which would be highly inefficient.
Without knowing your gameplay mechanics I can't be sure if you can use
OverlapPoint efficiently. - I came up with those doubts because you would still need to know somehow against what
Entity/Collider/Player... to check collisions.