Search Unity

TerrainHeight to Vector3 (SampleHeight ERROR)

Discussion in 'Scripting' started by FabrizioSpadaro, Oct 19, 2014.

  1. FabrizioSpadaro

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287

    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??
    Code (CSharp):
    1. TreeInstance pTI = new TreeInstance();
    2.             pTI.position = new Vector3(Random.value,0,Random.value);
    3.             float randomvar=Random.Range(-0.3f,1.0f);
    4.             pTI.widthScale  = 1+randomvar/2;
    5.             pTI.heightScale = 1+randomvar/2;
    6.             pTI.color = Color.white;
    7.             pTI.lightmapColor = Color.white;
    8.             pTI.prototypeIndex = Random.Range(0,2);
    9.             GameObject col = (GameObject) MonoBehaviour.Instantiate(Coll,Vector3.Scale(pTI.position,_Terrain.terrainData.size),Quaternion.identity);
    10.             float height = _Terrain.SampleHeight(col.transform.position);
    11.             col.transform.position = new Vector3(col.transform.position.x,height,col.transform.position.z);
    12.             _Terrain.AddTreeInstance(pTI);
    13.             col.name = _Terrain.terrainData.treeInstances.Length.ToString();
    14.             _Terrain.Flush();
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    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.
     
  3. FabrizioSpadaro

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287
    it's not an error is just that the collider has not been set correctly at the y worl point
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
  5. FabrizioSpadaro

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287
    if i have a problem i need to solve the problem no to use other solution ;) ...
    I need separate colliders to interact with the trees like raycast etc..
     
    Last edited: Oct 19, 2014
  6. FabrizioSpadaro

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287
  7. FabrizioSpadaro

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287
  8. TwistOfFat3

    TwistOfFat3

    Joined:
    Oct 10, 2014
    Posts:
    7
    Terrain.SampleHeight

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

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287
    did you read my post before posting this??
    o_O
     
  10. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Is the Coll center at the bottom of the collider?
     
  11. FabrizioSpadaro

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287
    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
     
  12. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    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 :)
     
  13. FabrizioSpadaro

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287
    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)
     
  14. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    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.

    Code (CSharp):
    1.     public Terrain terrain;
    2.  
    3.     void Update()
    4.     {
    5.         Vector3 newPos = transform.position;
    6.         newPos.y = terrain.SampleHeight (transform.position) + terrain.transform.position.y;
    7.         transform.position = newPos;
    8.     }
    Terrain.GetPosition() would also work.
    Since it does the same.
    Code (CSharp):
    1.     public Vector3 GetPosition()
    2.     {
    3.       return this.transform.position;
    4.     }
     
    Last edited: Oct 21, 2014
  15. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    So the center of the collider would be at the terrain height...
     
  16. FabrizioSpadaro

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287
    no..
    i'm not sampling the wrong terrain,i tried your code but same error
     
  17. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    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
     
    Last edited: Oct 22, 2014
  18. FabrizioSpadaro

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287
    i uploaded a video about it...
     
    Magiichan likes this.
  19. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Are you procedurally modifying the terrain? If so, are you sure it's not changing again after the colliders positioning?
     
  20. FabrizioSpadaro

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287
    ...... hhahahha i'm an idiot.
    yes that was the error thanks you so much
     
  21. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Oh my god... FINALLY! Q_Q
     
  22. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327