# How to initiate a prefab with correct Y position on a terrain

Hello,

I am trying to initiate prefab objects (Trees) on a terrain. The problem I am facing is that the trees are initiated at Y=0 position. So some of the trees are beneath the terrain, some trees appears at half of their height and so on. I want the initiated trees to be placed according to the terrain height. I couldn’t find a way to get the correct Y position of the terrain, where the trees are created. Below is the code I wrote. True, at the moment I set the Y position to 0, but my question is what to put at the Y position so every tree will be created at correct height.

void createTrees()
{
if ((dropsCount >= minDrops2Count) && (doCreate == 0))
{
for (int i = 0; i < numTrees; i++)
{
Vector3 position = new Vector3(egoPosX * Random.Range(0.8f, 1.42f), 0, egoPosZ * Random.Range(0.8f, 1.6f));
cloneTrees = Instantiate(prefab, position, Quaternion.identity) as GameObject;

``````            //script for fadein
material.color = new Color(1f, 1f, 1f, 0f);
//print("Create Trees function executed");
//print(material.color);
//end of fadein script

doCreate = 1;
time = Time.time;
Destroy(cloneTrees, 40f); //Destroy trees after ## sec
}
``````

So there are several ways to go about this. I will give you the idea I have come up with off the top of my head, it is likely not the best solution but it should work. The following are basic steps, you will have to figure out exactly how to do them in the way that you want to.

1. Put a collider on your terrain.

2. Shoot a ray from a location above your terrain downwards towards your terrain. As an example say your terrain has a maximum height of y50. The origin of your ray would be at a given x and z co-ordinate and y 51. So if you want the tree at x10 z20 you’d shoot a ray from the position (10,51,20) downwards. If you are orienting everything to the default world positions you can just use vector3.down which should be the down direction for the world.

3. Store the location that the raycast hits the terrain (you might want to tag the terrain or use a layer and use a comparison to make sure it’s the terrain you are hitting and not something else).

4. Instantiate your tree at that location that you stored.

5. Continue through your list of locations until all the tress you want to create have been created.

A few additional notes: As I said in the beginning there are multiple ways to tackle this, I came up with this on the spot and it is likely not the best way at all, it’s not very simple and probably not very efficient, but it should work. Also using a mesh collider can slow things down if your mesh is very complex. Lastly, if you don’t care about the trees being random you could just place empty or invisible game objects and use their locations as the places to instantiate your trees so you can ensure that they don’t go through the terrain, that approach might be even easier but you will give up the ability to randomly distribute the trees.

Good luck.

Thnx very much for the quick responses…

I still not clear about that…
In my script, I initiate several trees clones in random places (x, z) so where can I insert the ‘GetHeight’ function so every clone will get the correct positional height ?

Thnx