Search Unity

Check if the target collider is within a specific region of my collider

Discussion in 'Scripting' started by libra34567, Feb 8, 2018.

  1. libra34567

    libra34567

    Joined:
    Apr 5, 2014
    Posts:
    62
    So I'm in a case where my collider have a left detection range of 30 degrees, and right detection range of 60 degrees. And when there's a collision enter event, i only want my collider to call a function if the incoming collider is within my detection range.

    How should i achieve that?

    Any suggestions would be appreciated.
     
  2. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Surprisingly, there doesn't seem to be a way to manually check two colliders against each-other.

    However, you could achieve this by making the left and right regions separate colliders, and seeing which one the incoming collider hits.

    Alternately, if either of the colliders involved is a basic type (box, sphere, or capsule), you can use the Physics.Overlap___ methods to test collision with them.


    If I'm misunderstanding and this is a "vision cone" thing or similar, that's simpler - just use Transform.InverseTransformPoint to put the incoming object in the guard's local space, then use Mathf.Atan2 to see what the relative angle is.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    As already mentioned, I'd use separate colliders and verify the object is within one of your detection range colliders and then call whatever function.
     
  4. libra34567

    libra34567

    Joined:
    Apr 5, 2014
    Posts:
    62
    @Errorsatz @Joe-Censored Thank you guys for your suggestions, probably i didn't explain this good enough, what I meant is similar to vision cone. I later used the SignedAngle function to achieve it. Thank you for mentioning the keyword, it saved me a bunch of time !