TreeInstance is not working properly
void GenerateTreeColliders()
{
for (int i = 0; i < _terrainData.treeInstanceCount; i++)
{
Vector3 _newPos;
_newPos = _terrainData.treeInstances[i].position;
GameObject _collider;
_collider = new GameObject();
_collider.gameObject.AddComponent<BoxCollider>();
_collider.transform.position = _newPos;
}
}
Why are the positions of the objects created?
screenshot-17.jpg
(161.2 kB)
Comment
Best Answer
Answer by bahadir147 · Jul 10, 2017 at 11:59 PM
TerrainData data = terrain.terrainData;
float width = data.size.x;
float height = data.size.z;
float y = data.size.y;
foreach(TreeInstance tree in data.treeInstances)
{
Vector3 position = new Vector3(tree.position.x * width, tree.position.y*y, tree.position.z * height);
}
Please try :)
void GenerateTreeColliders() {
for (int i = 0; i < _terrainData.treeInstanceCount; i++)
{
Vector3 _newPos;
_newPos = Vector3.Scale (_terrainData.treeInstances[i].position,_terrainData.size)+_terrain.transform.position;
GameObject _collider;
_collider = new GameObject();
_collider.gameObject.AddComponent<BoxCollider>();
_collider.transform.position = _newPos;
}
Thank you, I solved you :)
I edited by myself
You're welcome, congratulations.
$$anonymous$$olay Gelsin.