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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

OnTriggerEnter not working properly sometimes, but alt+tab fixes it

Discussion in 'Editor & General Support' started by klad, Jul 22, 2011.

  1. klad

    klad

    Joined:
    Jul 13, 2011
    Posts:
    17
    Hello,

    I'm having a very strange issue lately. There are some times, when i'm playing my game, that my character just stops triggering the OnTriggerEnter/OnTriggerExit events of all my triggers. He keeps colliding with the terrain though, it's just the OnTriggerEnter/Exit that does not get called. Sometimes, it's only the OnTriggerExit that does not get called, but i'm still getting the OnTriggerEnter.

    I have found that if i alt-tab the game, everything works fine again. So i'm wondering, what does "alt-tab" do, that fixes this issue? Maybe it "flushes" somehow the state of the triggers ?

    I would appreaciate any help on this topic, because it's giving me headache already hehe.
     
    MominAman likes this.
  2. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    Never heard bout that one before. Maybe you could file a bug.
     
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I remeber having the same problem. It was because I manually set the position of the rigidbody that touched the triggers, instead of just using the physical movements. How are you moving your character?

    If you leave the window and go back, Unity probably calls a few initialization functions.
     
  4. klad

    klad

    Joined:
    Jul 13, 2011
    Posts:
    17
    I move my character using the Character controller that comes with unity.
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Are you moving the objects with the triggers?
     
  6. klad

    klad

    Joined:
    Jul 13, 2011
    Posts:
    17
    No, triggers are fixed objects in position. These triggers have only a box collider and a script. But I've realized that I can fix this error if I add a RigidBody component to the trigger with the isKinematic parameter checked. It makes it work but just with some triggers, in other triggers it's still not working.

    It is a new error, could it be due to that my scene is getting too big?
    Any suggestion?

    Thanks a lot
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Honestly, no idea why that is not working. The size should not have a negative impact, unless it produces a low frame rate.
     
  8. klad

    klad

    Joined:
    Jul 13, 2011
    Posts:
    17
    Hi, this error is produced with a low frame rate. I've optimized the scene and now it's working. But anyway, it's already very strange. I think that a low frame rate can make that trigger takes longer to fire the event but it should be fired after some seconds... frame rate in this zone is around 20fps inside the editor.

    We'll try to optimize some zones in our game
    Thanks a lot!
     
  9. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,196
    I'm having this same issue, when the fps is low OnTriggerEnter sometimes won't get called, I think it depends on the rendered frames, if the collider is moving (or the collider that is supposed to enter the trigger collider is moving) and in none of the frames rendered the non-trigger collider was inside the trigger collider, then the OnTriggerEnter will not get called...
    I have no idea how I will circumvent this in case fps drops...
     
  10. Farwall

    Farwall

    Joined:
    Sep 19, 2014
    Posts:
    18
    I'm having this same issue, when the fps is low OnTriggerEnter sometimes won't get called, Odds are that my object was moving too fast for the trigger to be calculated, effectively being positioned over the platform one frame and below it the next. You could do 1 of 2 things
    1. Clamp the downward/y movement of the player character so it can only move so quickly

    2. Since the triggers use the physics update you could increase the frequency of the fixed timestep http://docs.unity3d.com/Manual/class-TimeManager.html
     
    Rodolfo-Rubens likes this.
  11. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,196
    Ah that's a good solution too!
    But I forgot to post here but I fixed this by moving the object changing its velocity directly instead of using transform.Translate, that fixed this issue, seems like Translate only updates objects transform when the frame changes but objects moving with velocity is updated every time, or at least 60 updates per second, even if the frame don't change.
     
    PHL1 likes this.
  12. Kudry

    Kudry

    Joined:
    Jun 8, 2016
    Posts:
    1
    I had the same problem, OnTriggerEnter suddenly started to fail even if the objects did not move.
    I changed fixed timestep to 0.01 and now it works good.
    Thanks ^^
     
  13. unity_iJdCMepZRpRVDw

    unity_iJdCMepZRpRVDw

    Joined:
    Jan 4, 2019
    Posts:
    2
    These issues occur because of the delay in physics calculations done by the unity, all the physics related calculations are done in a fixed interval of time that is inside a fixed update function in the unity, consider a bullet which has a capsule collider and script to check on trigger event and if you move the bullet using update function even if the bullet collides with the some of the objects, on trigger events won't get called all the time because you are moving bullet using update function but unity physics calculation are done in fixed update so in some frame even though the bullet collides with the obstacle but fixed update called after some delay in that frame collision won't be triggered, so in order to resolve it you should use fixed update to move the bullet at that time both the bullet movement and physics update will be in sync and always trigger functions are called whenever there is a collision.

    But in some condition even though you are moving the object using fixed update if the velocity(speed in which object is moving) is too high then at that time also trigger events are not called all the time. that happens because even though you are moving object using fixed update but inside fixed update you are giving a large velocity to move the object even though the object position is updated in fixed interval because of large velocity it will travel large distance in small time in that fraction of time unity physics calculation may not be done so trigger events won't be called in that frame. but for this issue in unity 2019 there is a solution, in 2019 version they have introduced a new mode in rigid body i.e "Continuous Speculative" set it to this mode and all the calculation to move the game object(i.e adding force or by updating the character transform) should be there in fixed update function.
    (note: unity doesn't provide 100% guarantee of collision detection(mentioned in unity documentation in a detailed way) still there are some situations where an object can tunnel through the collider without triggering anything for more info you can go through unity documentation on the rigid body where they have mentioned everything)
     
    Last edited: May 10, 2019
    Tivise likes this.