Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Detect whether 2 2d objects are overlapping

Discussion in 'Meta-forum Discussion' started by maty1996, Apr 29, 2019.

  1. maty1996


    Nov 7, 2018
    Hello Unity community,
    I am in midst of building a 2d platformer, sidescroller game. Feature I am trying to implement are spikes - a sprite which will NOT contain a boxcollider2d and is walkable. Those spikes, when player is going to walk over, will trigger an action "taking damage".

    Here is my trouble. I cannot use boxcollider2d because those spikes are not an object that stops player for travelling. Instead it is an "area" where player will take damage. I need to check if the player objects collides / overlaps with the spikes.

    I really like that OnCollisionEnter2D class, although I cannot use it in this instance as I need to stay away from boxcolliders. I know I may be able to use Physics2D for this task, perhaps you may provide me some guidance.

    It would also be very handy if I could access the gameobject that the player overlaps with. Just like I did at the code below.

    private void OnCollisionEnter2D(Collision2D collision)
    if (collision.gameObject.tag == "spikes")
    damage damageScript = collision.gameObject.GetComponent<damage>();


    Additional note. I think I maybe able to use Physics2D.OverlapArea. Can you confirm this? Will it work without boxcolliders, and also will I able to access the gameobject.
  2. CrystalConflux


    May 25, 2017
    This is a scripting question. Meta-forum is for discussing the forum.

    You might have more luck posting in the scripting section.