- Home /
Could not create tree colliders. Maybe there are more Trees then PhysX can handle?
Hi all,
I have recently been exploring methods to create trees on the terrain via script.
Terrain terrain = (Terrain)GameObject.FindObjectOfType(typeof(Terrain));
tempInstance=terrain.terrainData.treeInstances[0];
for (int i=0;i<100;i++)
{
for (int j=0;j<100;j++)
{
tempInstance.prototypeIndex = 0;
tempInstance.color = new Color (1, 1, 1);
tempInstance.lightmapColor = new Color (1, 1, 1);
tempInstance.heightScale = 1;
tempInstance.widthScale = 1;
tempInstance.position = new Vector3 ((float)i/100, 0, (float)j/100);
terrain.AddTreeInstance (tempInstance);
terrain.terrainData.SetHeights(0, 0, new float[,] { { } });
terrain.Flush ();
}
}
The problem is that the collider disappears when I run the script. As such the character just falls through the terrain. The trees are created ok. If I stop the game mode, disable the script and play (with all the trees still three) the collider is there and works just fine. Furthermore if I create only 100 trees (ie set i,j=10) the collider works just fine. Any idea of how to fix this?
Thanks
Answer by RoloJo · Nov 20, 2013 at 03:35 PM
Figured out my problem.
If you dont delete the trees that already exist on the map you constantly just add trees with the above script. Eventually you exceed the maximum number of trees allowed on the map which crashes the PhysX. Instead use the following code which just "redefines" where trees are, how many etc:
Terrain terrain = (Terrain)GameObject.FindObjectOfType(typeof(Terrain));
tempInstance=terrain.terrainData.treeInstances[0];
tempInstance.color = new Color (1, 1, 1);
tempInstance.lightmapColor = new Color (1, 1, 1);
tempInstance.heightScale = 1;
tempInstance.widthScale = 1;
ArrayList instances = new ArrayList();
for (int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
{
tempInstance.position=new Vector3 ((float)i/10, 0, (float)j/10);
instances.Add(tempInstance);
}
}
terrain.terrainData.treeInstances = (TreeInstance[])instances.ToArray(typeof(TreeInstance));
terrain.terrainData.SetHeights(0, 0, new float[,] { { } });
Answer by MalachiteBR · Nov 14, 2013 at 07:39 PM
Well, Im not home so I cant test it. But first of all, I think you should move you treeInstance declaration inside the for loop and also make it var treeInstance = new TreeInstance(); instead of the first tree.
As for the position I think you should use like fixed x, or y, and then increment the x/y according to the loop, if you are trying to make some kind of tree line.
The problem is that I dont know if it will make a copy of this instance tree or it will use the same instance, that will cause many problems.
Your answer
Follow this Question
Related Questions
Terrain Tag OnControllerColliderHit not showing 0 Answers
Creating Box Collider On Specific Terrain Textures 1 Answer
Animated Object falls through elevated terrain 0 Answers
How to create a ground/terrain/floor that won't respond to gravity or objects falling on it? 1 Answer
Solution for the FPS Character Controller falling through terrain 2018.3 Version 0 Answers