Multiple Terrains: Which one is my object over?

I have created multiple terrains in the same scene (using World Creator). This resulted in a total of 9 terrain GameObjects in the scene. I also created a hovercraft to fly over the terrains. My question is: how do I know which of the terrains my hovercraft is over (because I am going to change the hovercraft configuration based on this)?

My best idea so far: I can create an array of all the terrains. I know the size and relative location of the terrains. I could use the hovercrafts position to calculate the terrain I must be over. This does not seem very elegant. I would prefer to query the terrain objects themselves, but I have not seem anything in the scripting API that seems relevant.

Better ideas?

Your idea, or even raycasting down would work

I wonder how Terrain.activeTerrain is chosen, and whether that would work.

This is what I learned about Terrain.activeTerrain from researching the forums. I don’t know how to confirm or deny it. I did not save the link, but you should be able to find the original via search:

“You can’t control which terrain is referenced by the activeTerrain property, it’s set as the first terrain that was loaded. Use GetComponent() to get the Terrain component attached to the particular terrain game objects you have. That way you can access each individual terrain uniquely. The activeTerrain property is just a more convenient way of doing things in those cases where you have a single terrain.”

I ultimately did use raycasting and that worked fine. I will post my code as an example that might help someone in the future.

Thanks for the recommendation!

I ended up using Physic.Raycast and that works fine. I have included my code below, in case someone in the future has this question. I put all the Terrains in a Terrain layer. “vehicleObject” flies over the terrain. FixedUpdate would probably be a better choice, but I just like CoRoutines.

public class VehicleControl : MonoBehaviour {

public GameObject vehicleObject;

void Start () {
     StartCoroutine ("getVehicleParameters");
}

IEnumerator getVehicleParameters () {
     Transform vehicleTransform = vehicleObject.GetComponent <Transform > ();
     float rayLength = 2000.0f;
     RaycastHit hit;
     int terrainTileMask = 1 << LayerMask.NameToLayer ("Terrain");

for (;;) {
     if (Physics .Raycast (vehicleTransform .position , vehicleTransform .forward ,
         out hit, rayLength, terrainTileMask )) {
              print ("Hit Distance: " + hit.distance + " Name: " + 
                      hit.collider.name +  " Tag: " + hit.collider.tag + 
                      " Vehicle Position: " + vehicleTransform.position);
     } // end if

     yield return new WaitForSeconds (.2f);

} // end for

} // end CoRoutine}

Need to create a tag called Terrain and set the all your terrains tags to Terrain.
Need to be standing on a terrain for call to work. 10.0f is depending on testing objects height.
Place this script on whatever object you will be using to test with.

;------------------------------

public class GetTerrain : MonoBehaviour {

private string testTerrain;
public string currentTerrain;

void Update () {

RaycastHit hit;
if (Physics.Raycast(this.transform.position, Vector3.down, out hit, 10.0f))
if (hit.collider.gameObject.tag == “Terrain”) {
testTerrain = hit.collider.name;
if (testTerrain != currentTerrain) {
currentTerrain = testTerrain;
Debug.Log(currentTerrain);
/// ------------------------------

// … Do fantastic things!

/// ------------------------------
}
}
}
}