Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Physics.SphereCast ignoring collider if too far away?

Discussion in 'Physics' started by Bandinopla, Sep 9, 2018.

  1. Bandinopla

    Bandinopla

    Joined:
    Feb 2, 2018
    Posts:
    21
    I have this setup where i shoot a ray and when it intersects a sphere collider i will add a particle system.
    It works fine if the distance is somewhat closer, if i move the character away, it doesnt detect the hit...

    in the picture below you can clearly see the trail of the ray pass through the collider without any call to OnCollisionEnter...

    the weird thing is that it seems to be a matter of distance, if y manually move the character closer and closer there comes a point where every ray hits... any idea why this might be happening?

    upload_2018-9-9_17-3-25.png
     
  2. Bandinopla

    Bandinopla

    Joined:
    Feb 2, 2018
    Posts:
    21
    here is a better example...
     

    Attached Files:

  3. Bandinopla

    Bandinopla

    Joined:
    Feb 2, 2018
    Posts:
    21
    SOLVED!! My bad! i was not calling the raycast method correctly, the params were in the wrong order, i was putting the "layer index" in the place of "max distance" that's why it felt like it was a distance issue...

    After the "out hitInfo" it goes the maxDistance and THEN the layer index...

    Posting the solution in case someone makes the same mistake!
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    To be fair, the multiple raycast methods are not very well thought out. Probably could have half the number of methods and it would save time even if more verbose, than battling the intellisense that died etc.
     
  5. Bandinopla

    Bandinopla

    Joined:
    Feb 2, 2018
    Posts:
    21
    Another mistake i did was to manually type the layer index instead of calling LayerMask.GetMask which returns a diferent number than the one listed in the layers panel because of the bit masking mechanic.