Search Unity

How to check if a collider overlaps with sphere?

Discussion in 'Scripting' started by FeastSC2, May 9, 2020.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I have a list of colliders I need to check against.

    I don't want to use any of the Physics methods because those check against every colliders and it would be too expensive.

    How can I know if Collider A overlaps with a sphere B of a certain radius?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    What kind of collider is A? If they're both spheres it's as simple as checking if the distance between their centers is less than the sum of the two spheres radii.

    You can also use layer masks to optimize Physics.Overlap operations.
     
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    What would be best is that it worked with any of the Collider's base class aka with spheres, box, capsules, terrain collider, mesh colliders.

    Is there no way provided by Unity to check if a collider overlaps a sphere?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    There is but they're all contained in the dreaded Physics class that you are avoiding.

    The other options are to use the collider callbacks such as OnTriggerEnter etc..
     
  5. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I understand that problems can usually be tackled with OnTriggerEnter however I'm in a specific situation.

    I have already discovered colliders with OnTriggerEnter in a certain area around my particle system.

    And now, I want to discover if a particle overlaps one of the colliders found in OnTriggerEnter.

    I'm trying to have particles detect triggers, you can see more about that in this thread: https://forum.unity.com/threads/par...ecting-particle-velocity.881008/#post-5792485

    The problem is that OnParticleTrigger does not provide which trigger it has entered.. So I must somehow find the trigger it has collided with myself.
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleTrigger.html
     
    Last edited: May 9, 2020
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    So just to be clear, the particle is the sphere in this question?

    Is the particle small enough that we could get away with just treating it as a dimensionless point instead of a sphere?

    Unfortunately I don't think there's a one-size-fits-all approach that will match all collider types. How pinpoint accurate does this have to be?
     
  7. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Yes

    No, the ideal would be that it's a sphere with a custom radius present in the Particle System's trigger module.
    https://i.imgur.com/EYT1GV6.gifv