Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved on Trigger Enter sometime miss the trigger !

Discussion in 'Physics' started by Rafaat_Ashour, Jan 24, 2023.

  1. Rafaat_Ashour

    Rafaat_Ashour

    Joined:
    Sep 25, 2020
    Posts:
    50
    Hello all,

    i have issue with my game, the object sometime miss the trigger which is the player in my case, i though at first maybe its issue with the curve setting or the box collider size have issue. after checking its not both of them fine.

    on the player i have a box collider and is trigger its checked and a rigidbody with is kinematic checked too.
    the upcoming object also have box collider and is trigger not checked and a rigidbody with is kinematic checked too.

    the collision detection set to Discrete, i change it to continues and also try continues dynamics but still miss some object sometimes.

    both of my object in the same layer i created and i try disable all layer physics to avoid any issue but still miss.

    lastly i create a new scene where object span on same x position as my player and run the game so see without moving my player is there's going to be a miss and they miss again.

    what's weird that in the editor if i pause the game and drag the miss object back to re enter the trigger it will detected !
    does anyone have idea why this happen ? or its a bug in the editor

    am using unity 2021.3.16f
    here's a video for the issue, both object should be on the same color to collect it

    https://www.youtube.com/shorts/XowUrT1mTQM
     
  2. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    I believe that's because continuous collision detection doesn't apply to triggers.

    Some ideas:
    1) Decrease the fixed time step (Time settings)
    2) Increase the character collider length (towards the camera)
    3) Increase the object length (away from the camera)
    4) Use a boxcast and detect those obstacles manually
     
  3. Rafaat_Ashour

    Rafaat_Ashour

    Joined:
    Sep 25, 2020
    Posts:
    50
    thank you for your reply, i solve my issue.

    i was using transform.translate to move my object which not good for physics, i change it to velocity and now the continuous collision work 100% fine.
     
    JustinNaicker likes this.
  4. JustinNaicker

    JustinNaicker

    Joined:
    Jan 4, 2023
    Posts:
    47
    Yup, you are essentially teleporting your object, and it isn’t hitting the trigger. Good catch.
     
    Rafaat_Ashour likes this.
  5. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    You could be teleporting the body (via Transform) and still be able to hit the trigger. My theory is that something else is responsible for this behaviour.

    That tells me that the problem is elsewhere. I totally understand what you mean, although i must say that, technically speaking, continuous collision detection (CCD) still doesn't apply here because you are testing a rigidbody against a trigger.
    Watch this example:

    All bodies use CCD. It doesn't matter if they are kinematic (MovePosition + transform.position) or dynamic (velocity + transform.position), the result is the same. Here is the "mover" script:
    Code (CSharp):
    1. [RequireComponent(typeof(Rigidbody))]
    2. public class BodyMover : MonoBehaviour
    3. {
    4.     [SerializeField]
    5.     bool _useTransform;
    6.  
    7.     [SerializeField]
    8.     Vector3 _velocity;
    9.  
    10.     Rigidbody _rigidbody;
    11.  
    12.     private void Awake()
    13.     {
    14.         _rigidbody = GetComponent<Rigidbody>();
    15.     }
    16.  
    17.     private void FixedUpdate()
    18.     {      
    19.         if (_useTransform)
    20.         {
    21.             transform.position += _velocity * Time.fixedDeltaTime;
    22.         }
    23.         else
    24.         {
    25.             if (_rigidbody.isKinematic)
    26.                 _rigidbody.MovePosition(_rigidbody.position + _velocity * Time.fixedDeltaTime);
    27.             else
    28.                 _rigidbody.velocity = _velocity;
    29.         }
    30.     }
    31. }

    When a body enters the trigger, its color changes to red (as shown in the video).
    Code (CSharp):
    1.  
    2. public class OnTriggerEnterScript : MonoBehaviour
    3. {
    4.     private void OnTriggerEnter(Collider other)
    5.     {
    6.         print($"OnTriggerEnter with {other.name}");
    7.         other.gameObject.GetComponent<MeshRenderer>().material.SetColor("_Color", Color.red);
    8.     }
    9. }

    My theory
    Since i haven't seen the code, i can only guess the problem was caused by a combination of two factors:
    1. You are probably implementing movement in Update.
    2. The displacement applied (with transform.position as you already mentioned) doesn't include a deltaTime. In other words, your movement could be "frame rate dependent":
    Code (CSharp):
    1. // Frame rate dependent
    2. transform.position += fixedDisplacement;
    3.  
    4. // Frame rate independent
    5. transform.position += velocity * Time.deltaTime;
    Why velocity works?
    When you set a velocity, the body doesn't move instantly. In order for the body to be moved, it needs to wait for the physics simulation to act on it. By introducing (a fixed?) velocity, you eliminated that frame rate dependent movement, even if you are still using Update.

    --> Apologies if none of these "you are using this and that" are actually true. Keep in mind i'm just giving you my theory :p.
     
    Rafaat_Ashour likes this.
  6. JustinNaicker

    JustinNaicker

    Joined:
    Jan 4, 2023
    Posts:
    47
    Maybe so, I was just assuming based on OPs description (I know it isn’t teleporting instantly). But your theory is quite interesting, and might be a better explanation than mine.
     
    lightbug14 and Rafaat_Ashour like this.
  7. Rafaat_Ashour

    Rafaat_Ashour

    Joined:
    Sep 25, 2020
    Posts:
    50
    thank you for your attention, to be honest i got curious about it too.
    after checking my code, the main issue was not using transform, or not using continuous collision it was the update method, i was using update and after change it to FixedUpdate with the transform.translate it work fine, i don't know how i miss that !
    i put it back to transform.translate instead of velocity because as i know transform performance better than velocity.

    thanks again dude ;)
     
    lightbug14 likes this.