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

Do Triggers Need Rigidbodies? (Beginner Question)

Discussion in 'Physics' started by MoistDumpling14, May 3, 2019.

  1. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Hey guys, just a pretty simple question, do all triggers need to have a rigidbody? Say you have non-moving invisible trigger that when the player steps on it, opens a door. Would I need to give this object a rigidbody/kinematic rigidbody?
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

     
    MoistDumpling14 likes this.
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    No, this object doesn't need a rigidbody. Only the player should have a rigidbody.
     
    zioziozio and MoistDumpling14 like this.
  4. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Okay, could you explain this for me also, from the unity manual: Even when immobile, kinematic rigidbody colliders have different behavior to static colliders. For example, if the collider is set to as a trigger then you also need to add a rigidbody to it in order to receive trigger events in your script. If you don’t want the trigger to fall under gravity or otherwise be affected by physics then you can set the IsKinematic property on its rigidbody.
     
    Last edited: May 4, 2019
  5. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Okay, could you explain this for me also, from the unity manual: Even when immobile, kinematic rigidbody colliders have different behavior to static colliders. For example, if the collider is set to as a trigger then you also need to add a rigidbody to it in order to receive trigger events in your script. If you don’t want the trigger to fall under gravity or otherwise be affected by physics then you can set the IsKinematic property on its rigidbody.

    Just make it easier for me to understand what this is saying.
     
    Last edited: May 4, 2019
  6. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    Several parts of the manual regarding colliders and rigidbodies are outdated and come from the Unity 5 era. As for today, static colliders raise trigger events when any other rigidbody (either kinematic or not) contacts them.

    For example, I use static colliders (without rigidodies) in racing tracks for detecting shortcuts. Each collider is marked as Trigger and includes a script that listens to the OnTriggerEnter event. Works flawlessly:

    upload_2019-5-4_12-16-39.png
     
    Last edited: May 4, 2019
    Judgeking and MoistDumpling14 like this.
  7. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    So just to clarify, you put the OnTriggerEvent script inside the actual trigger itself, and if it detects an object with a tag of "racecar" then it will react to it and do something?
     
  8. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    And I am just caught up on that sentence above ("even when immobile, kinematic colliders are different to static...") that I don't understand. Are you telling me that sentence is outdated or something. If it's not could you explain it to me word for word cause the wording in the sentence is just confusing.
     
  9. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    Yes, I've put a script in the same GameObject as the trigger itself. The script contains a method "OnTriggerEnter". This method is called whenever any Rigidbody touches the trigger. The method receives the information of the colliding object, so you can take your decisions based on the tag or any other property.

    Are you seeing the correct documentation? I can't find anything like that in the Unity docs. Maybe you're reading the docs for a (very) past version of Unity:

    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
     
    MoistDumpling14 likes this.
  10. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    The docs are accurate afaik: One of the entities is required to have a Rigidbody. It's simply a matter of where you put it. Do you want it on the trigger? the other? both? Does anyone care? You decide.

    If you have a niche case regarding kinematics/statics then you should clarify the conditions and give an example of something that is not working.
     
  11. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    This sentence is confusing though cause it sounds like it’s saying that all triggers need to have a rigid body. The wording in the sentence is kind of confusing and I don’t know what it’s trying to say, I’m just a beginner. I just want to know what this means in really simple terms.
     
    Tonymotion likes this.
  12. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Yes, it is in the kinematic rigidbody collider section in the second paragraph: https://docs.unity3d.com/Manual/CollidersOverview.html
     
  13. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    Oh, ok. That information requires an update. It doesn't mention that you can make kinematic colliders interact seamlessly with other colliders with the Contact Pairs Mode option in the Physics settings:

    https://docs.unity3d.com/Manual/class-PhysicsManager.html
    I always configure "Enable All Contact Pairs" in my projects so kinematic bodies work the same way as non-kinematic ones.
     
    MemeDream likes this.
  14. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    What is confusing? It seems fairly straightforward to me - simply one of the Colliders involved has to have a Rigidbody also.

    If you have an overlapping pair of colliders then at least one of them must have a Rigidbody to activate the collision and trigger events as needed for everyone involved. Consider a bunch of buildings in a city. They don't need rigidbodies because the active rigidbodies moving around during gameplay are going to be the ones 'triggering' events when they are needed. Are you not seeing this be true in your tests?
     
  15. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    It feels to me like this is saying that if you have a collider that's a trigger, then you also need to add a rigidbody to it, but that's not always true. I don't really understand the example it's giving and I also don't understand how immobile kinematic rigidbodies are different than static colliders. Could you explain it super simple and easy of a word for word of the sentence I don't understand. Sorry I don't get it, I'm still learning.
    Is it just saying that if you have a trigger then somewhere in the equation there needs to be a rigid body?
     
    Last edited: May 6, 2019
  16. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    Forget that information. It's incomplete (see my post above).

    Super-simple:
    1. Open Project Settings > Physics. Configure "Contact Pairs Mode" to "Enable All Contact Pairs".
    2. Add Rigidbodies to the physically moving objects only.
    3. Use static triggers, static colliders, OnTriggerEnter, etc normally. Any project, any platform (desktop, mobile, etc)
     
    Rickmc3280 and crawfis like this.
  17. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    How exactly are immobile kinematic rigidbody colliders different than static colliders?
     
  18. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Edy is more familiar with the technicals, but I can explain what I know

    Static colliders are supposed to be things that never move. You flag them as static and expect it to never change. This lets some calculations process them faster because it knows they shouldn't ever be moving. An example would be a bunch of buildings, or props, or terrain.

    Kinematic is just a Rigidbody that does not receive forces. An example would be a character that you control explicitly and don't want other forces interfering. Drop a kinematic box into a pile of non-kinematic boxes and it will plow them completely out of the way no matter what their mass is. If it's 'immobile', I assume you mean it's just not moving. In that case, it's just a Rigidbody that isn't moving and isn't going to be moved by random forces hitting it. Pretty simple.
     
  19. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    So the sentence above that I was so confused on is wrong. Because not all triggers need rigidbodies to receive trigger events, yet it still says "For example, if the collider is set to as a trigger then you also need to add a rigidbody to it in order to receive trigger events in your script". Sorry if I'm wrong but that's what I've been so confused about.
     
    Tonymotion likes this.
  20. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    It's not really the colliding I'm worried about but I get what you're saying. Tell me though, if this option was not enabled and not possible to change, what is this sentence trying to say?
     
  21. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Yes, that information is wrong, as Edy pointed out. The document we linked you is correct.

    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
     
    MoistDumpling14 likes this.
  22. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Okay that helps, on the link that you just gave me it states that the message gets sent to the rigidbody and the collider. What exactly is this "message" and what is the difference between the two of these. Shouldn't it just send the message to the script that the gameobject involved is attached to?
     
  23. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    The physics engine watches Rigidbodies and their latest position to see if they are touching any other colliders, those other colliders might be static, might be triggers, or might be attached to a Rigidbody - or not.

    If it sees a an overlap between a Rigidbody and something else then the Rigidbody processor will go "Ok, Rigidbody X is touching/overlapping Collider Y, lets fire the appropriate messages for everyone involved in this collision."

    So it checks the colliders involved and sends the messages to any script on those objects who are implementing the methods it is currently sending, such as OnTriggerEnter(), or OnCollisionEnter(). This is a component-based-architecture so these get sent to any Monobehavior Component on that Collider's Object.

    There are tutorials for this sort of thing in the Learn section of the website.
     
    Edy and MoistDumpling14 like this.
  24. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    That makes perfect sense! Could you explain the final sentence though? Like... what is component based architecture and what is a monobehavior component
     
  25. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
  26. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
  27. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Okay this is my final clarification. Basically, when one object with a rigidbody collides into another object (could be static, kinematic, trigger, etc.) the physics engine figures out what message needs to happen. In this case it is a message that gets sent to both objects involved in the trigger event. If they are using methods (OnTriggerEnter, etc.) that are correlated to the message that gets sent then you can use the methods to run your code. Is this all correct?
     
    Last edited: May 8, 2019
    Bulbousonions13 and Tonymotion like this.
  28. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    Yep, that's it.