Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to place trees during runtime?

Discussion in 'Scripting' started by willemsenzo, Aug 2, 2014.

  1. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    I'm trying to place trees during runtime but when I spawn them they can't be seen visually. When I debug I can tell the trees are being added to the treeInstances array. The documentation doesn't have any examples on how to actually do this but it appears to be pretty straightforward. If there is anyone who can point me in the right direction I would appreciate it. Btw this is how I'm trying to do this.

    Code (csharp):
    1.  
    2. TreeInstance treeInstance = new TreeInstance();
    3. treeInstance.prototypeIndex = 0;
    4. Vector3 position = spawnPos;
    5. position.y = terrain.terrainData.GetInterpolatedHeight(terrainX, terrainZ);
    6. treeInstance.position = position;
    7. terrain.AddTreeInstance(treeInstance);
    8. terrain.Flush();
    9. print(terrain.terrainData.treeInstances.Length); //does show trees are being added to the treeInstances array
    10.  
     
    Last edited: Aug 3, 2014
  2. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    597
    Are you actually doing something with the trees after putting them in your array? I dont see any for loops or anything that are cycling through all of the instances in the array.
     
  3. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
  4. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    I can be wrong (never played with trees yet) ... but perhaps you have to add treeInstance to terrain.terrainData.treeInstances instead of treeInstanceList.

    Or you can use terrain.AddTreeInstance(treeInstance).
     
    Last edited: Aug 2, 2014
    Magiichan likes this.
  5. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    I indeed didn't check the scale and colour I sort of assumed they were optional but not mandatory to make it work, I will try it out.

    Thanks for the tip Fraconte I actually did use terrain.AddTreeInstance that but I placed the wrong code here lol. I will change it right away.
     
  6. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    I tried setting the heightscale/widthscale and color but this doesn't make any difference. I already spent 3 days on this...
     
  7. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    Ok I have found the problem after searching for a long time. Basically the position I assigned to the tree was incorrect so it didn't spawn where I expected it. I didn't realize a position on the terrain has a value between 0.0 and 1.0 because treeInstance.position just asks for a Vector3. All I had to do was just calculate (1.0f/terrainResolution) * terrainTileX.
     
    ambid17 and Fraconte like this.
  8. RedGolpe

    RedGolpe

    Joined:
    Sep 26, 2015
    Posts:
    2
    Also note that heightscale and widthscale should be defined too as they take default value of 0 otherwise.