- Home /
Programatically placed trees end up in a wrong place
I am working on a terrain generator, I got my terrain going, so I decided to play around with trees.
I add 5 tree prototypes to the terrain data like so:
List< TreePrototype > treePrototypes = new List< TreePrototype >();
foreach ( GameObject treePrefab in this.treePrefabs )
{
TreePrototype treePrototype = new TreePrototype();
treePrototype.prefab = treePrefab;
treePrototypes.Add ( treePrototype );
}
terrainData.treePrototypes = treePrototypes.ToArray();
Then, I have an array of points that are within the terrain and I would like to place trees in those locations. I try to do so using:
foreach ( Vector3 point in spawnPoints )
{
Vector3 onTerrainPosition = point;
RaycastHit hit;
if ( Physics.Raycast( point, -Vector3.up, out hit ) )
{
if ( hit.transform.name == "Terrain" )
{
onTerrainPosition = hit.point;
}
}
TreeInstance newTree = new TreeInstance();
newTree.position = onTerrainPosition;
newTree.widthScale = 1;
newTree.heightScale = 1;
newTree.color = Color.yellow;
newTree.lightmapColor = Color.yellow;
newTree.prototypeIndex = Random.Range ( 0, terrainData.treePrototypes.Length );
Terrain.activeTerrain.AddTreeInstance( newTree );
}
All this works, except that all the trees are at "Tree Instance at (1.0, 0.0, 1.0)".
I thought maybe it's because terrain hasn't been placed in the scene yet, but I delayed the tree placement call and get the same result.
If during run time I click on "Mass Place Trees" everything works - I get tons of trees all over the place.
What could cause the positions to be off/reset/wrong? Maybe those locations don't have enough space? Any thoughts?
Thanks!
I also tried creating a completely flat terrain to see if terrain deformations causes collisions problems with trees... nope, trees are still at (1,0,1)
strange that it works fine after the level has loaded.
What controls the vector3 of spawnPoints? is that getting made too late, or if you are using a Random.Range(1, someNumber) for the x & z values is the someNumber getting defined too late?
Also, it probably wont change it, but you could add a large vertical vector to the point in the raycast so that you can still hit higher terrain like Physics.Raycast(point + Vector3.up * 50f, -Vector3.up, out hit)
Debug.Log everything. You can't really see what's happening otherwise.
I generate a texture for high map, those spawnPoints are sampled from it. I use this code to debug those points and they seem to be right:
Gizmos.color = Color.yellow;
if ( spawnPoints != null )
{
foreach ( Vector3 spawnPoint in spawnPoints )
{
Gizmos.DrawSphere ( spawnPoint, 1.0f );
}
}
Unless of course terrain space is not the same as the world space...
hmm well I would think that the Gizmos would use world space since you didn't call it with the terrain object.
so if the tree generation works after the scene has been created can you do something like Invoke("$$anonymous$$akeTrees", 2f); in the start. just to make sure everything is in place already.
Answer by KonstantinMaximus · Feb 09, 2015 at 12:20 AM
So, I figured it out. Tree positions have to be in 0 to 1 space. So, I just did this:
newTree.position = new Vector3 ( point.x / this.mapWidth, 0, point.z / this.mapDepth );
Your answer
Follow this Question
Related Questions
Why are the trees dark when you put them to the scene and zoom in? 1 Answer
Tree Creator Problems 1 Answer
Can't use tree brush tool 0 Answers
Tree brush making very tiny trees 0 Answers