Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question OnTriggerEnter() / OnCollisionEnter() mechanic

Discussion in 'Documentation' started by hiephuypro112, May 8, 2023.

  1. hiephuypro112

    hiephuypro112

    Joined:
    Sep 11, 2022
    Posts:
    3
    Hi everyone,

    I'm building my item collecting system when this question comes to my mind. I'm working on limiting the distance that my player can interact with a near-enough item. Currently I'm checking if the player is within range using Vector2.distance and check it every single frame.

    Of course it's not a very efficient way to do this, as I'm re-calculating the distance every frame and that is definitely a resource waste. Therefore, I'd just go back to the traditional way which is to have a sphere collider wrapping around my object and notify if a player has made a collision to it.

    A question comes to me is how Unity has implemented OnTriggerEnter(). Wouldn't they just have to do the same thing to check for collision between rigid bodies? Or it's some kind of physics detection that I haven't heard of. If the implementation is that they would have to run codes in a FixedUpdate() to check for collisions, then wouldn't it just simply the same approach with same resource-consumed level, not even count in the Gizmo UI.

    Any thought on this?
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,877
    OnCollisionEnter works between 2 rigidbodies. Does it give any issues?
     
  3. hiephuypro112

    hiephuypro112

    Joined:
    Sep 11, 2022
    Posts:
    3
    Well I'm not exactly having any issue, just curious about how Unity implement those functions, for performance purpose.