Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

OnTriggerEnter2D Issues?

Discussion in '2D' started by SUNTheBaer, Feb 20, 2021.

  1. SUNTheBaer

    SUNTheBaer

    Joined:
    Jun 17, 2020
    Posts:
    2
    Hello!
    I have a question about OnTriggerEnter2D. So I'm working on a black hole type of item for my game, and if I use it, it works fine the first time, turns on the collider, sucks the enemy toward the player, and turns off the collider, but if I use it a second time without moving and while the enemy is still within the area of effect, the method doesn't get called. Only once I move from the starting position does Unity recognize that the enemy is in the collider, is there a reason why this happens or how I should go about fixing this?
     
  2. MilesAway1980

    MilesAway1980

    Joined:
    Oct 11, 2014
    Posts:
    13
    Have you looked into OnTriggerStay2d? This will be called as long as the collider is inside the trigger.
    OnTriggerEnter2D is only called when it first enters. The collider has to leave and come back to be called again.
     
  3. SUNTheBaer

    SUNTheBaer

    Joined:
    Jun 17, 2020
    Posts:
    2
    Well I don't want the method to be called multiple times in a single instance. I'm currently adding a force OnTriggerEnter2D, however the collider turns off after a certain point. I then turn it back on, and it doesn't trigger. So does turning a collider on, off, and on again not supposed to call the Enter method?
     
  4. MilesAway1980

    MilesAway1980

    Joined:
    Oct 11, 2014
    Posts:
    13
    That makes sense.

    I don't know if that's how Unity works with triggers. It might still have the collider registered as inside because it never left, even though you deactivated it.

    With what you're trying to do, you may still need to use OnTriggerStay2D, and then use a boolean to see whether it's already been applied.

    Another thought is to put the collider on a child object, and then after you deactivate the collider, move the child object outside of the trigger and back in again so it registers.

    Just brainstorming.
     
unityunity