TerrainHeight to Vector3 (SampleHeight ERROR)


Hy guys i’m using this script to geneare a collider for each trees.
The x and z pos are correct but i can’t figure out how to make the y works correctly…
this is my code where is the problem??

TreeInstance pTI = new TreeInstance();
            pTI.position = new Vector3(Random.value,0,Random.value);
            float randomvar=Random.Range(-0.3f,1.0f);
            pTI.widthScale  = 1+randomvar/2;
            pTI.heightScale = 1+randomvar/2;
            pTI.color = Color.white;
            pTI.lightmapColor = Color.white;
            pTI.prototypeIndex = Random.Range(0,2);
            GameObject col = (GameObject) MonoBehaviour.Instantiate(Coll,Vector3.Scale(pTI.position,_Terrain.terrainData.size),Quaternion.identity);
            float height = _Terrain.SampleHeight(col.transform.position);
            col.transform.position = new Vector3(col.transform.position.x,height,col.transform.position.z);
            _Terrain.AddTreeInstance(pTI);
            col.name = _Terrain.terrainData.treeInstances.Length.ToString();
            _Terrain.Flush();

What’s the error that you’re getting?
Please show it to us. I am not familiar with the terrain api, but I think that I’d be able to help out if I knew what line generates the error.

it’s not an error is just that the collider has not been set correctly at the y worl point

http://answers.unity3d.com/questions/13324/i-can-walk-through-trees.html
You might want to do this if u want colliders.

if i have a problem i need to solve the problem no to use other solution :wink:
I need separate colliders to interact with the trees like raycast etc…

up

up

Terrain.SampleHeight

Samples the height at the given position defined in world space, relative to the terrain space.

did you read my post before posting this??
:eyes:

Is the Coll center at the bottom of the collider?

no the collider is at the center of the gameobject, the problem is that when i instantiate the collider prefab, his x and z position is right but the y isn’t

Documentation says about Terrain.SampleHeight:
Samples the height at the given position defined in world space, relative to the terrain space.

Not sure, but i believe you should add your terrain y pos to this line:
float height = _Terrain.SampleHeight(col.transform.position) + _Terrain.GetPosition().y;

offtop: i had to think for awhile for this one: Vector3.Scale(pTI.position,_Terrain.terrainData.size) …thats clever :slight_smile:

I’ve already tried this:
+ _Terrain.GetPosition().y;
but same problem… (and i think that line should be added only if your terrain has y != 0)

Maybe you’re sampling the wrong terrain?
Cause I sampled the height of a terrain that isn’t placed in the scene center, and it worked perfectly fine.
I moved a cube along the x & z axis.
All height were sampled correctly.
And obviously, if you move the terrain upwards, you’ll
have to add the terrain’s y position, just like @TwistOfFat3 & @chelnok said.

    public Terrain terrain;
 
    void Update()
    {
        Vector3 newPos = transform.position;
        newPos.y = terrain.SampleHeight (transform.position) + terrain.transform.position.y;
        transform.position = newPos;
    }

Terrain.GetPosition() would also work.
Since it does the same.

    public Vector3 GetPosition()
    {
      return this.transform.position;
    }

So the center of the collider would be at the terrain height…

no…
i’m not sampling the wrong terrain,i tried your code but same error

What error? ._.
Can you show us multiple screenshot of the scene(from different perspectives)?
I’d like to see how the position isn’t set correctly. is the offset fixed?
Also, make sure that the terrain rotation is set to 0, 0, 0

i uploaded a video about it…

1 Like

Are you procedurally modifying the terrain? If so, are you sure it’s not changing again after the colliders positioning?

3 Likes

… hhahahha i’m an idiot.
yes that was the error thanks you so much