Search Unity

Physics.RayCast not working for terrain ?

Discussion in 'Physics' started by Moddingear, Jun 19, 2016.

  1. Moddingear

    Moddingear

    Joined:
    Apr 1, 2016
    Posts:
    29
    I'm having an issue where my Physics.Raycast doesn't cast on terain, so I use both Physics.Raycast and Collider.Raycast, but most of the time, they doesn't detect the terrain.
    Is there a setting to tweak or something to do in order to make it work ?
    Here is the code i use to make my raycasts :
    Code (CSharp):
    1.     public static bool CompleteRaycast(Ray ray, float maxDistance, out RaycastHit raycastHit, LayerMask layer)
    2.     {
    3.         RaycastHit rhray;
    4.         bool hitted = false;
    5.         if(Physics.Raycast(ray, out rhray, maxDistance, layer))
    6.         {
    7.             hitted = true;
    8.         }
    9.         List<RaycastHit> terrainHits = new List<RaycastHit>();
    10.         foreach (TerrainCollider col in terrains)
    11.         {
    12.             RaycastHit rh;
    13.             if(layer != (layer | (1 << col.gameObject.layer)))
    14.             {
    15.                 continue;
    16.             }
    17.             if(col.Raycast(ray, out rh, maxDistance))
    18.             {
    19.                 terrainHits.Add(rh);
    20.                 hitted = true;
    21.             }
    22.         }
    23.         raycastHit = new RaycastHit();
    24.         if (hitted)
    25.         {
    26.             RaycastHit closer = rhray;
    27.             foreach (RaycastHit hit in terrainHits)
    28.             {
    29.                 if(hit.distance < closer.distance)
    30.                 {
    31.                     closer = hit;
    32.                 }
    33.             }
    34.             raycastHit = closer;
    35.         }
    36.         return hitted;
    37.     }
    Should I use a small SphereCast instead ?
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    I have no problems at all with Raycasts and terrains. Maybe the layer setup is wrong?

    - Ensure that your layer mask includes the layer of the terrain
    - Terrains must NOT reside at layer 2. Raycasts ignore this layer.
    - Ensure that your Raycast is not colliding with some trigger before reaching the terrain (maybe your config for raycast-triggers allows them to be detected by raycasts).

    I must say that I always use Physics.Raycast and not TerrainCollider.Raycast. If all the above is correct, then I'd recommend you to create a very simple scene with a single terrain and a simple script for verifying whether TerrainCollider.Raycast works correctly or not. If not, then I'd recommend you to file a bug including that simple scene.
     
  3. Moddingear

    Moddingear

    Joined:
    Apr 1, 2016
    Posts:
    29
    The only layer I exclude is the layer 8, otherwise all layers are included. But there seems to be "holes" were neither physics.raycast nor terraincollider.raycast works. And I've got this bug since 2 versions.
    I'll try to upload an example of this bug.
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Does Physics.Raycast work for you?
    As alternate solution, you might use Physics.RaycastAll (or better Physics.RaycastNonAlloc) and then discard all collided elements except the terrain.
     
    Moddingear likes this.
  5. Moddingear

    Moddingear

    Joined:
    Apr 1, 2016
    Posts:
    29
    I'm so sorry : It was a bug with a rigidbody and a non-convex mesh collider that made any script unable to add a force when the mesh was touching the terrain.
    Still a bug, but not with the same origin at all.
     
  6. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    How do you do these kinda things, im no coder but im have issues with a fast moving ball going through my box collider, ive tried everything ive read about using raycast to stop this from happening but im no coder
     
  7. Moddingear

    Moddingear

    Joined:
    Apr 1, 2016
    Posts:
    29
    Set the rigidbody collsion detection from Discrete to Continous dynamic.
     
  8. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    None of those things work :/ ive tried that, ive tyred the "dontgothroughthings" script nothing is working
     
  9. Moddingear

    Moddingear

    Joined:
    Apr 1, 2016
    Posts:
    29
    Create another thread for this.
     
  10. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    Already did
     
  11. JaimeCollazo07

    JaimeCollazo07

    Joined:
    Dec 5, 2019
    Posts:
    4
    If anyone is still on here I'm having a similar issue. I'm recasting on my terrain and its only reading it every other time not every time. Any advice thanks
     
    FoxNerdSaysMoo likes this.