Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Sphere casts don't hit triggers. What other options do I have?

Discussion in 'Physics' started by invicticide, Feb 7, 2015.

  1. invicticide

    invicticide

    Joined:
    Nov 15, 2009
    Posts:
    109
    In my game, nearly all colliders are triggers, because I need to know when things touch but I don't want Unity resolving the collisions (i.e. stopping motion, applying forces, etc.)

    I have a situation now where I need to do a sphere cast against those triggers. This, of course, doesn't work, as stated clearly in the docs. But that doesn't change the fact that I need to do it.

    So, what other options do I have? I see three right now, none of them good:
    1. Fire a cluster of raycasts instead, to approximate the shape of the sphere. This sucks because a) it's a crap-ton of extra collision tests and b) it's going to miss small things in the collision area that slip between the rays.
    2. Give every trigger a second, child collider, which is of the same type and size but with isTrigger=false, and put all these duplicate colliders on a separate layer that nothing collides with (not even itself). Sphere cast against this "special" layer. This would work (in theory) but duplicating all my collision seems pretty inefficient.
    3. Change my entire collision setup over to the 2D collision system, since the docs don't say anything about Physics2D.CircleCast not hitting triggers. (Can any Physics2D users confirm that behavior?) This arguably makes sense in the long run since my game is in fact 2D, but I'd really rather not overhaul the entire collision system just to make this one test possible...
    Any other options or suggestions I'm missing?
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,355
    What is the sphere cast doing, context wise?

    You might be able to use a trigger shell around your objects and detect when that trigger enters other triggers if it's about proximity and almost-collisions. But if your cast is checking across long distances, or finding the next/nearest object, etc, that obviously isn't a good solution...
     
  3. invicticide

    invicticide

    Joined:
    Nov 15, 2009
    Posts:
    109
    It'll be the collision test for a beam weapon like this. (That example fires along world X, but other use cases could fire at any arbitrary angle.)
     
  4. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,355
    You might be able to swap in Rigidbody.SweepTest? I'm actually not sure offhand if that collides with triggers or not. http://docs.unity3d.com/ScriptReference/Rigidbody.SweepTest.html

    Otherwise I think the best solution here is actually to use a trigger for the beam weapon itself. In Unity 4.x, make sure you make it a kinematic rigidbody too (or will have performance penalty on moving it around).

    Apply damage in OnTriggerStay, so it accumulates when the object is held in it, and use OnTriggerEnter/Exit for effects or other start/stop uses...
     
  5. invicticide

    invicticide

    Joined:
    Nov 15, 2009
    Posts:
    109
    Thanks for the suggestion (creating a trigger for the beam weapon itself). Not sure why that didn't occur to me; in retrospect, that is of course the correct way to do it.

    I've got that implemented now and it's working swimmingly. :)