- Home /
Unity 5 Terrain TreeInstance Scaling VS Colliders [Scripting]
When making a new TreeInstance to be added to a terrain in a script, modifying the scaling property seems to cause the colliders to behave improperly for some trees. Here is my quick and dirty runtime tree placing code (replicates very roughly what Mass Place Trees does):
t.terrainData.treeInstances = new TreeInstance[0];
for (float i = 0; i < terData.heightmapHeight; i++) {
for(float j = 0; j < terData.heightmapWidth; j++)
{
if(t.terrainData.GetSteepness(j / t.terrainData.alphamapWidth, i / t.terrainData.alphamapHeight) < 35.0f)
{
if(Random.Range(0, 100) < 15)
{
float perturbfactorx = Random.Range(0,2)*0.001f;
float perturbfactorz = Random.Range(0,2)*0.001f;
int prototypes = terData.treePrototypes.Length;
TreeInstance TI = new TreeInstance();
TI.position = new Vector3((j/terData.heightmapWidth) + perturbfactorx,
(terData.GetHeight((int)j, (int)i) / 1000f) - 0.002f,
(i/terData.heightmapHeight) + perturbfactorz);
TI.widthScale = 1;
TI.heightScale = 1f;
TI.color = Color.yellow;
TI.lightmapColor = Color.yellow;
TI.prototypeIndex = Random.Range(0, prototypes);
t.AddTreeInstance (TI);
}
}
}
}
t.terrainData.RefreshPrototypes ();
t.terrainData.SetHeights (0, 0, new float[0, 0]);
The variable "t" in this instance can be set to Terrain.activeTerrain if you're playing along at home. This code works perfectly (at least on my Perlin terrain that is 2000x, 1000y, 2000z), unless I change the following:
TI.heightScale = 1f;
to
TI.heightScale = 1.5f;
The trees still look fine, behave well with lighting, and get their colliders, but the colliders don't seem to scale, they are just centered on the now much taller trees. As a result, small objects can slide through the base of some of the trees without colliding. It's a pretty interesting effect, actually, but I'd like to know how to make these colliders scale with my trees, too.
Additional information, I am testing this using two standard Unity tree prefabs: Broadleaf_Desktop and Conifer_Desktop. My target is Windows PC.
All help is appreciated. Thanks!
Interesting question. Looking through Intellisense, I don't see collider interfaces. An alternative is to use the terrain treedata positions, shift to world space and create the capsule colliders manually. To avoid the double penalty of treePrefab collider (but not scaled, too small to help) + the manually created collider, might be worth removing the prefab collider.
...and I just found this post
http://answers.unity3d.com/questions/713772/mass-place-trees-scale-issue.html
which is puzzling because it's modifying heightScale as you did. If you added terrain.Flush(), I wonder if yours would have worked.
Your answer
Follow this Question
Related Questions
Tree colliders in Gaia (procedural worlds) 0 Answers
How to make trees in a terrain transparent in runtime? 0 Answers
How to make trees impassable? 1 Answer
how to add collider to trees made with terrain tool? 1 Answer
How to put colliders on trees 0 Answers