Search Unity

SphereCastall sometimes does not hit colliders; what gives?

Discussion in 'Physics' started by rageingnonsense, Dec 23, 2015.

  1. rageingnonsense

    rageingnonsense

    Joined:
    Dec 3, 2014
    Posts:
    99
    I recently made a post about SphereCastAll hitting inactive colliders. The documentation was cleared up, and it made sense to me how it was behaving.

    I decided I could use this "quirk" to my advantage for some code I am working on. However, I find that when my inactive gameobjects are within a certain distance from camera (a sweet spot), hits do not register.

    What I am doing is working on some terrain deformation code for chunked LOD (not Unity terrain). The idea is that chunks of the highest LOD get deformed, and then the deformation is propagated to lower levels of detail. This works wonderfully, with the exception of this sphere cast issue.

    I am expecting the spherecast to detect these higher level chunks even if they are not currently active. This DOES work, but not in this damned sweetspot. Consider the following image:



    The colors signify the changes in LOD. The terrain is given a basic bubbly shape to eliminate the possibility of hits not registering due to terrain being too flat.

    The mesas are areas I was able to deform on mouse click from this camera position (using a radius of 20). You can see that I was able to deform not only the chunks I am close to, but chunks in the distance (orange) several detail levels difference from the highest (in purple).

    The area in the center though, I clicked all over that and it would not deform. Debugging showed it is because the spherecastall I am using is not detecting the highest LOD chunks. It does not matter if I make them visible or not manually, the spherecast does not hit them from here. If I were to move my camera forward a bit, then they would start to register.

    I know it is detecting inactive ones in general because I could create mesas in the distance. But for whatever reason the ones in this "sweet spot" are not detected at all! some are detected, but not the ones with the highest LOD. You can even see where the mesa did not fulyl form because the spherecastrall call did not detect the other chunks in the area.

    What could be causing this? Why is spherecastall not working consistently? I feel like this is a legitimate bug.

    Before anyone asks; they are mesh colliders and they are not set as triggers. The spherecastall originates from about 100 meters from the terrain. Here is the call:

    Code (csharp):
    1.  
    2. RaycastHit[] hits = Physics.SphereCastAll(point + (transform.up * (maxHeight + 1)),
    3.   radius,
    4.   -transform.up,
    5.   maxHeight + Mathf.Abs(minHeight) + 1,
    6.   LayerMask.GetMask(layerName));
    7.  
    point is a position on the terrain. maxHeight is about 100. Cast distance is about 151. the layermask is absolutely correct and matching the layer the chunks are on.
     
  2. rageingnonsense

    rageingnonsense

    Joined:
    Dec 3, 2014
    Posts:
    99
    Nobody? I can't be the only person who has experienced this.
     
  3. rageingnonsense

    rageingnonsense

    Joined:
    Dec 3, 2014
    Posts:
    99
    I sort of figured out what was causing this issue for me. Posting the explanation here for anyone else who gets confused.

    The problem was rooted in the active state of the colliders, and scene loading. when I first loaded my scene, the active states of the colliders were being set in Start(). In this first frame, setting collider state doesn't seem to fully work! Even though inactive colliders existed (checkbox in inspector was uinchecked), these didn't actually apply until it was switched after the first frame. When my LOD levels were changed, this was triggered, and the colliders were set appropriately. I was not noticing this issue earlier obviously.

    My solution was to add a new layer for inactive terrain, and set ignorecollisions for this layer. In this way, I can raycast against the colliders (and never change the actual active state), while also ignoring collisions.

    I do think that the active state of colliders in the first frame is a bug though.
     
    Dantus likes this.