Search Unity

SphereCastAll returns (0,0,0) for all RaycastHit points

Discussion in 'Physics' started by IntelligentDesign, Aug 29, 2016.

  1. IntelligentDesign

    IntelligentDesign

    Joined:
    Jun 13, 2014
    Posts:
    51
    As the title would suggest, having a major issue with SphereCastAll returning BAD information for the hit points of each of the RaycastHits. The offending code is here:
    Code (CSharp):
    1. void ClampToStairs() {
    2.  
    3.         Ray dwnRay = new Ray (rb.position + new Vector3(0, groundCollider.radius, 0), Vector3.down);
    4.         RaycastHit[] clampHits = Physics.SphereCastAll (dwnRay, 1, groundClampDist, groundCollisionMask);
    5.  
    6.         if (clampHits != null && isGrounded) {
    7.             RaycastHit lowestContact = clampHits[clampHits.Length - 1];
    8.             foreach (RaycastHit hit in clampHits) {
    9.                 print (hit.collider.name);
    10.                 print (hit.point);
    11.                 if (hit.point.y <= lowestContact.point.y)
    12.                     lowestContact = hit;
    13.             }
    14.             print ("lowestContact point = " + lowestContact.point);
    15.             print ("lowestContact = " + lowestContact.collider.name);
    16.             rb.position = lowestContact.point ;
    17.       }
    18. }
    I've already gone through the obvious troubleshooting options (made sure the appropriate objects are in the corresponding layer for the groundCollisionMask layer mask, parenting and unparenting the objects I'm attempting to collide with, etc.) and some not-so-obvious troubleshooting options (setting the radius of the sphereCast to a simple 1 unit when it should really be set to groundCollider.radius) and nothing works. The print statements DO return the correct names of the objects the sphereCasts hit, but the hit.point position is returned as (0,0,0) in every case.

    Does anyone know a solution? Barring that, does anyone know of a similar code package that DOES have correct functionality (considering that there is no sane reason why a basic Physics method shouldn't work out of the box)?
     
  2. IntelligentDesign

    IntelligentDesign

    Joined:
    Jun 13, 2014
    Posts:
    51
    So I've looked over the Docs for SphereCastAll again and there's a section with the following:

    Now this shouldn't be the case in my original code since I had radius set to groundCollider.radius - groundCollider.contactOffset, but it would appear that is what's happening if I'm getting (0,0,0) returned where it isn't expected. Does anyone know of a potential workaround?
     
  3. davidrochin

    davidrochin

    Joined:
    Dec 17, 2015
    Posts:
    72
    Thank you for pointing out that. It was very useful to me.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I wonder what's preventing Unity from returning at least initial cast point (ray.position / position)?
    I mean, we already got distance. If that distance <= 0 we can simply perform a different check if that is the case.

    Why introduce a potential bug instead?
     
  5. FileThirteen

    FileThirteen

    Joined:
    Oct 23, 2012
    Posts:
    40
    Foreman_Dev, dkydev and DaDarkDan like this.
  6. Foreman_Dev

    Foreman_Dev

    Joined:
    Feb 4, 2018
    Posts:
    82
    Hey, thanks for this thread. I just ran into this issue when switching from Physics.SphereCast to Physics.SphereCastNonAlloc, which I needed to properly handle projectile impact detection in my game. This was SO annoying to deal with. My projectiles were impacting at position (0,0,0) for no apparent reason.

    Why the heck does SphereCastAll (and SphereCastNonAlloc) behave differently than SphereCast? o_O I expected consistent behavior: don't return a hit if any SphereCast starts inside/overlapping a collider.

    This is poor design and should really be fixed. I can't believe the issue tracker says "By Design". At the very least there should be a bool parameter for SphereCastAll and SphereCastNonAlloc to define whether or not we want it to behave the same as SphereCast.

    I had to design a custom way to filter out invalid hits (hits with zero distance) from my hit array anytime I do a Physics.SphereCastAll or Physics.SphereCastNonAlloc, which costs me extra overhead during an operation I need to do potentially many times each frame based on the amount of activity in my game session. :(
     
    Last edited: Mar 30, 2024