- Home /
SphereCastAll is passing through colliders selectively
I'm trying to SphereCast between two objects to tell if a clean line of sight exists. You can see how my colliders look in the second picture. The center collider is on the root transform, and the outside ones are within child transforms. My code is very simple, it's:
public bool TestValidPlacement(Transform st, Transform et) {
collisionTestingEndpoints[0] = st.position;
collisionTestingEndpoints[1] = et.position;
Vector3 dir = et.position - st.position;
dir = dir.normalized;
float dist = Vector3.Distance(st.position, et.position);
Ray ray = new Ray(st.position, dir);
Ray backRay = new Ray(et.position, -dir);
RaycastHit[] sHits = Physics.SphereCastAll(ray, width/2f, dist);
RaycastHit[] sBackHits = Physics.SphereCastAll(backRay, width/2f, dist);
Debug.DrawRay(ray.origin, ray.direction * dist, Color.cyan, 10f);
if (sHits.Length > 0 || sBackHits.Length > 0) return false;
return true;
}
private void OnDrawGizmos() {
Gizmos.DrawSphere(collisionTestingEndpoints[0], width / 2f);
}
By all accounts this collision should get picked up by SphereCastAll. The start point of the ray is clearly outlined by the gizmo, showing that it isn't initially overlapping any colliders. And the Debug ray clearly passes through two colliders. Both of which are on the default raycast layers. Even if somehow the forward SphereCastAll was initially inside a collider, then the backwards SphereCastAll would absolutely pick it up. But neither return any hits for this setup. Any help is appreciated.
Code should be ok.
few things to check..
width >0?
colliders are not in trigger mode? (or check from Project settings that Queries hit colliders is checked)
Is it intentional to return false if ShpereCast hits colliders?
The width parameter is greater than 0, you can see the width parameter being used to do the SphereCast is the same width used to draw the gizmo, so you can see how big the SphereCast is in the photo. I just checked and verified the colliders are not in trigger mode. I have a "Queries hit Triggers" option that is checked, but no checkbox that says "Queries hit Colliders", however my layer collision matrix is all check marks, everything should collide with everything else. I am intentionally returning false if there is a collision and true if there is not.
How it works is this, you can move the "resistors" by picking them up from the center. If there is a "wire" attached it runs the above function to test if there is a clear line of sight between the sphere and the same spot just outside of that other resistor. The weird part is that I can grab the other resistor and have perfect collision detection. The only difference is that one end of the wire attaches to a transform stored in the "start" member and the other end attached to a transform with the member named "end". It runs this exact same function to check for collisions (in both directions). I've even verified that the exact same transforms are being sent to the TestValidPlacement function in both the working example and the non working example you see above.
Strange.. I tested the code and both ways it worked just fine. If you have tested that rays are ok from both of these spots (the transform are correct) then I have no more ideas what could be the issue, sorry..
Btw. this might not be related to the issue, but in your 2nd picture there is some extra debug ray.