Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Continious OnCollisionEnter2D and OnCollisionExit2D

Discussion in '2D' started by RolfinatorNL, Apr 11, 2021.

  1. RolfinatorNL

    RolfinatorNL

    Joined:
    Mar 20, 2021
    Posts:
    4
    Hi,

    I try to detect if a RB is (still) on a tile, but while it's on there, it keeps triggering OnCollisionEnter2D and OnCollisionExit2D continiously.

    I've tried a few combinations, just BoxCollider2D, a TileCollider2d in combination with a CompositeCollider, but all show the same effect.

    Since i want to set a state to my Player if it enters it and leaves it, that doesn't work if both get keeping triggered..

    Any ideas?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,343
    It won't do that unless you're doing something that's causing it.

    TileCollider2D, CompositeCollider2D are not special and the physics system doesn't know anything about them. In the end, it'll only exit if it has no contacts with that collider or if you're doing something to cause it to cycle like disabling/enabling colliders etc.

    NOTE: There's a physics forum for this stuff.
     
  3. RolfinatorNL

    RolfinatorNL

    Joined:
    Mar 20, 2021
    Posts:
    4
    Let's say i have a square sprite with a box collider (that's easier for this discussion)
    I have a player (RB) that's moving over the collider. I would expect it to only exit if it's not longer touching the sprite?

    Further i don't to anything with disable / enable

    The square sprite has this code;
    The RB has continious colliding tracking..


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Safezone : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.    private bool hasEntered;
    9.  
    10.     void OnTriggerEnter2D(Collider2D hitInfo)
    11.     {
    12.         Debug.Log("Your in the Safezone!!!");
    13.     }
    14.  
    15.     void OnTriggerExit2D(Collider2D hitInfo)
    16.     {
    17.         Debug.Log("Your left the Safezone!!!");
    18.     }
    19.  
    20.  
    21. }
    22.  
    p.s. sorry will next time post it in the other forum..
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,343
    If you are getting Enter/Exit continuously with nothing changing and the player collider (whatever that is) still overlapping this other box then basically 2D physics is broken and there'd be pitchforks out.

    TBH, showing a script with enter/exit on it doesn't really show anything. What collider is on the player (not that this would really matter) and how is the Rigidbody2D being moved?

    Put together a simple example in a new project with two boxes and see if you can duplicate it there. If you get can send me a simple reprouction project and I'll take a look for you.