- Home /
Question by
Vecidive · Jan 30, 2013 at 08:07 PM ·
javascriptterraindistanceloop
How to get the treeInstances around the player?
Is there another way to get the tree IDs of terrain trees with a specific distance to the player? I ask this because the current script causes every second a game freeze. Would be nice if someone can help.
greets Vecidive
var distance : float;
var cplayer : CharacterController;
var vecreal : Vector3;
var treelist = new ArrayList();
var aTerrain : Terrain;
function Start () {
aTerrain = Terrain.activeTerrain;
StartCoroutine("FindTrees");
cplayer = GameObject.FindObjectOfType(CharacterController);
}
function FindTrees()
{
while(true)
{
yield WaitForSeconds(1);
treelist.Clear();
for(var treenumber=0; treenumber < aTerrain.terrainData.treeInstances.Length; treenumber++)
{
vecreal = Vector3.Scale(aTerrain.terrainData.treeInstances[treenumber].position,
aTerrain.terrainData.size) + aTerrain.transform.position;
distance = Vector3.Distance (cplayer.transform.position, vecreal);
if(distance<40)
{
treelist.Add(treenumber);
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613091851im_/https://answers.unity.com/themes/thub/images/avi.jpg)