Search Unity

Spawn ON terrain

Discussion in 'Scripting' started by DubStepMaster, Jul 29, 2013.

  1. DubStepMaster

    DubStepMaster

    Joined:
    Jul 17, 2012
    Posts:
    40
    Hey there

    I have following problem:
    I wrote a spawnsystem and it works fine.
    There is still one problem i can't solve yet:

    How i can spawn enemys ON the terrain?
    How i get the height of one point from the terrain?

    I tried it with:
    Code (csharp):
    1. pos.y = Terrain.activeTerrain.terrainData.GetHeight();
    but GetHeight only works for int variables and not for float (How it should).

    Thanks for help

    DubStep
     
    Last edited: Jul 29, 2013
  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Did you read the docs on the terrain system or try raycasting?
     
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
  4. DubStepMaster

    DubStepMaster

    Joined:
    Jul 17, 2012
    Posts:
    40
    ok
    no i didn't know this command, but now it doesn't work, too.

    Code (csharp):
    1. pos.y = Terrain.activeTerrain.terrainData.GetInterpolatedHeight(pos.x,pos.z);
    this is my code for setting the "right" height.
    pos is a vector3.

    now: what is wrong whis this?
     
  5. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Oh, this is what you need to use:

    Terrain.SampleHeight
    http://docs.unity3d.com/Documentation/ScriptReference/Terrain.SampleHeight.html
    Note that it is relative to the terrain position, so you may need to take that into account depending on your terrain height.
    http://answers.unity3d.com/questions/37144/incorrect-height-with-sampleheight.html
     
  6. DubStepMaster

    DubStepMaster

    Joined:
    Jul 17, 2012
    Posts:
    40
    RIGHT!

    Thank you very much!
     
  7. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Last time I looked, the terrain system docs were somewhat lacking, especially with heights and such. Been that way for a while. I can very much understand his confusion.
     
  8. the_Whizzkid

    the_Whizzkid

    Joined:
    Apr 15, 2020
    Posts:
    20