Search Unity

Raycast with multiple terrains not working.

Discussion in 'World Building' started by dwatt-hollowworldgames, Jul 13, 2020.

  1. dwatt-hollowworldgames

    dwatt-hollowworldgames

    Joined:
    Apr 26, 2019
    Posts:
    104
    I am trying to do a raycast to get an aircrafts altitude agl. however the raycast is always returning false. Also with multiple tiles sampleheight is returning the wrong value. Collisions with the terrain work fine but I have no way of knowing my height above the terrain. Any ideas why?

    Ray ray = new Ray(transform.position, Vector3.down);
    RaycastHit hit;
    if(Terrain.activeTerrain.GetComponent<Collider>().Raycast(ray, out hit, 5000))
    {
    _RadarAltitude = hit.distance;
    Debug.Log("Raycast distance = " + hit.distance);
    }

    Ray ray = new Ray(transform.position, Vector3.down);
    RaycastHit hit;
    if(Physics.Raycast(ray, out hit, 5000,11))// terrain is layer 11
    {
    _RadarAltitude = hit.distance;
    Debug.Log("Raycast distance = " + hit.distance);
    }

    neither of these work when I am using 10 x 10 10k x 10k tiles. If I use 1 100k x 100k tile it works just fine. My guess is the raycast is only done against the active terrain and not any of the other tiles.