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. Dismiss Notice

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)?
     
    unity_2bigostudio and hickna like this.
  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,292
    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
    dkydev and DaDarkDan like this.