Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Physics.Raycast not detecting terrain in scene

Discussion in 'Physics' started by tazakk, Dec 7, 2023.

  1. tazakk

    tazakk

    Joined:
    Jul 26, 2023
    Posts:
    1
    Hello,

    I have some terrain in my scene that I have added to the Terrain layer. It is not flat, so I am trying to use a Raycast in order to grab the y-coordinate for spawning entities. Here is the code I use for the Raycast:

    Code (CSharp):
    1. var maxAttemptsToSpawnOnNavMesh = 15;
    2. int layerMask = 1 << 8;
    3. while (maxAttemptsToSpawnOnNavMesh > 0)
    4. {
    5.     RaycastHit hit;
    6.     Vector3 upDir = transform.TransformDirection(Vector3.up);
    7.     if (Physics.Raycast(spawnPos, upDir, out hit, Mathf.Infinity, layerMask))
    8.     {
    9.         Debug.DrawRay(spawnPos, upDir * hit.distance, Color.green, 5f);
    10.         Debug.Log($"Raycast found position: {hit.point}\nUpdating y-value for spawn ...");
    11.         spawnPos.y = hit.point.y;
    12.         break;
    13.     }
    14.     else
    15.     {
    16.         Debug.DrawRay(spawnPos, upDir * 1000, Color.red, 5f);
    17.         Debug.Log($"Raycast did not hit. Retry number {16-maxAttemptsToSpawnOnNavMesh} ...");
    18.         spawnPos = partitionCenter + Random.insideUnitSphere * spawnRadius;
    19.         maxAttemptsToSpawnOnNavMesh -= 1;
    20.     }
    21. }
    For some reason, the raycast is not detecting the terrain and always returns false. I added the
    Debug.DrawRay()
    to see if maybe I was pointing it the wrong way, but it shows the ray going straight through the terrain layer. I've included a picture of the rays in the Scene view below:

    raycast_terrain.PNG

    Here is what I've tried:
    1. Moving the terrain to the Default layer.
    2. Including all layers by removing the LayerMask in the Raycast() call.
    3. Using Vector3.up directly instead of transform.TransformDirection(Vector3.up).
    4. I also added Mathf.Infinity to the Raycast() call since I didn't include it originally.
    I'm not too sure what else to try and thought I would ask here. I am very much a noob when it comes to Unity, so any help or explanation is appreciated.

    Thanks!
     
  2. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    105
    Are you raycasting upwards from a spawn position below the terrain?
    If so, I don't think rays hit the underside of the terrain unless you turn on Physics.queriesHitBackfaces?
    Or if you pick a point above the terrain and cast down, that should also work (and I'd guess "better" than turning on global backface thingy)?
    My kids are all home, so I'm operating at reduced mindpower, sorry in advance if this is obvious or unhelpful, but hopefully it fixes it for you:)