Box Collider 2D for Manual Detection of Collisions

To resolve collision issues in Unity, Solution 1 involves the following steps: Firstly, set Collision and Triggers according to the game being played (i.e., BoxCollider2D for 2D games) and ensure that BoxCollider2D trigger Boolean is checked. Secondly, inspect the Rigidbody Component attached to objects as Collision requires a physics component. For 2D games, RigidBody2D should be used and iskinematic should be disabled as Collision won’t work.
As a recommendation, it is suggested to use Unity’s internal physics system instead of relying solely on collision detection as shown in the script.


Question:

As my game is turn-based, collision detection is only required following a particular game event such as PlayerMoveComplete.

One possible solution would be to create a MonoBehaviour that represents a rectangle and then verify if the player is inside its parameters.

class MyRect {
 void OnMoveComplete () {
    // if player lies within the specified rectangle
    if (this.Contains(player.transform.position))
       DoSomething();
 }
}

I prefer having the ability to view and modify the content within the editor, resembling a two-dimensional MSDT A1.

I believe the default implementation of the box collider 2D could be utilized by me as well.

collider2D.bounds.Contains(pos)

I’m uncertain whether the calculations will still be performed by it, even if none of its message handling methods such as OnCollisionEnter2D are implemented. In case it does, it will create unnecessary workload.


Solution 1:

I presume your intention is to enhance the physics, which is the primary purpose of your inquiry.

To enhance the performance of your game that extensively employs physics components but doesn’t rely on frame rate-based physics, you can deactivate physics calculations and only simulate them when required using Physics.Simulate based on game logic.

Consider implementing Physics Best Practices to enhance the performance. Utilizing

LayerMask

s and optimizing raycasts can be beneficial. It is recommended to apply physics separately on
layers, collision matrix
to achieve better results.

As per your question:

It’s possible to cache colliders and later utilize them in components. Please note that the code has not been tested and may have some typos. Additionally, certain colliders may not possess `bounds`.

[RequireComponent(typeof(Collider2D))]
public class Interactable2D : MonoBehaviour
{
    [SerializeField] private Collider2D _collider2D;
    protected virtual void Interact()
    {
        // Do something
    {
    public void Interact(Entity/Collider/Player entity)
    {
        if (this._collider2D.OverlapPoint(entity.transform.position))
            this.Interact();
    }
#if UNITY_EDITOR
    private void Reset()
    {
        this._collider2D = this.GetComponent();
    }
#endif
}

Collider2D.OverlapPoint

The function referred to as

OnCollisionEnter2D

is essentially an event. While Unity may cache its reference, it’s not recommended to use reflection to invoke this method or other similar methods such as

Start

or

Awake

.

The invocation of the code is dependent on its implementation. Only the relevant calculations required to verify the point’s boundary will be included in the calculations for

OverlapPoint

.

My own suggestions

To improve collision detection efficiency, I recommend using Unity’s internal physics system instead of relying solely on

OverlapPoint

, as demonstrated in the script (without additional
collision detection
). This is because
game engines
typically employs OcTree(3D) or QuadTree(2D) to split collision detection into chunks. Otherwise, comparing collisions of each

Rigidbody/2D

with every

Collider/2D

would be O(n*m) (n – number of rigidbodies, m – number of colliders), which is highly inefficient. Please correct me if I am mistaken.

It’s difficult to determine the effectiveness of

OverlapPoint

without understanding your specific gameplay mechanics. My doubts arise because in order to detect collisions, you would also need to have knowledge of the

Entity/Collider/Player...

against which to check.


Solution 2:


One possible solution is to utilize Physics2D.OverlapBox() function to detect colliders that overlap within a specified range. Other alternatives are also available, such as Physics2D.OverlapCircle and Physics2D.OverlapArea. For more details, you can refer to the documentation provided by Unity.

  1. Check out the documentation for Unity’s 2D physics at the following link: https://docs.unity3d.com/ScriptReference/Physics2D.html.
  2. The documentation for Physics2D.OverlapBox can be found at the following URL: https://docs.unity3d.com/ScriptReference/Physics2D.OverlapBox.html

As an illustration, you have the option to simply state “OnCompleteMovement”.

//Assuming this is written inside player script
void OnMoveComplete () 
{
    // checks 0.5 radius area around player and if any collision with layer "CheckPoint" found then return that collider info
    Collider2D col = Physics2D.OverlapCircle(transform.position, 0.5f,LayerMask.GetMask("CheckPoint"))
    // if we have hit the checkpoint
    if (col!=null)
       DoSomething();
}

Frequently Asked Questions