Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Tree random creation / destruction around player using terrain object and treeInstances. I'm using it in a racing game.
// TREE Script - Generate random trees around player position, adjusting them to terrain height
// Script by: Manuel Fernández Yáñez - 9/5/2013 redgreenrefactor.blogsite.org @manufy
import System.Collections.Generic;
var treeList : List.< TreeInstance >;
var timer : float = 0;
var player : Transform;
function Start () {
// 1 - Get Tree Instances
var actualtreeInstances : TreeInstance[] = Terrain.activeTerrain.terrainData.treeInstances;
Debug.Log( "Removing Trees: " + actualtreeInstances.length );
/* OPTIONAL FOR DEBUGGING // 2 - List Tree Coords
for ( var t : int = 0; t < actualtreeInstances.length; t ++ ) {
Debug.Log("Existing Tree (XZ): " + actualtreeInstances[t].position.x + " " + actualtreeInstances[t].position.z + " Y: " + actualtreeInstances[t].position.y);
} */
// 3 - Initialize tree list and remove ALL Trees
treeList = new List.< TreeInstance >();
Terrain.activeTerrain.terrainData.treeInstances = treeList.ToArray();
Debug.Log( "Trees removed: " + actualtreeInstances.length + " Trees actual: " + Terrain.activeTerrain.terrainData.treeInstances.length);
// 4 - OPTIONAL: CREATE INITIAL TREES, OTHERWISE START WITH NO TREES
//Debug.Log( "Created Trees: " + " Trees actual: " + Terrain.activeTerrain.terrainData.treeInstances.length);
}
function Update () {
var treeposition : Vector3 = player.position;
var treeDisplacement: Vector3 = Vector3.zero;
var terrain = Terrain.activeTerrain;
// 1 - Create Tree from Prototype Tree 0
var newtree : TreeInstance = new TreeInstance();;
newtree.prototypeIndex = 0; // From terrain tree prototypes list index
newtree.color = new Color (1, 1, 1);
newtree.lightmapColor = new Color (1, 1, 1);
newtree.heightScale = 1;
newtree.widthScale = 1;
// 3 - Displace tree position randomly and height adjustment
treeDisplacement = new Vector3(Random.Range(-100, 100),0,Random.Range(-100, 100)); // World Space Coords Random around player transform
newtree.position = new Vector3(player.position.x + treeDisplacement.x,player.position.y + treeDisplacement.y,player.position.z + treeDisplacement.z );
newtree.position.y = terrain.terrainData.GetInterpolatedHeight(newtree.position.x, newtree.position.z); // Dont sure if I have to normalize coords
var newtreeterrainLocalPos = newtree.position - terrain.transform.position;
var newtreenormalizedPos = Vector2(Mathf.InverseLerp(0.0, terrain.terrainData.size.x, newtreeterrainLocalPos.x), Mathf.InverseLerp(0.0, terrain.terrainData.size.z, newtreeterrainLocalPos.z));
var newtreeterrainNormal = terrain.terrainData.GetInterpolatedNormal(newtreenormalizedPos.x, newtreenormalizedPos.y);
// 3 - Calculate player coords
var playerLocalPos = player.position - terrain.transform.position;
var playernormalizedPos = Vector2(Mathf.InverseLerp(0.0, terrain.terrainData.size.x, playerLocalPos.x), Mathf.InverseLerp(0.0, terrain.terrainData.size.z, newtreeterrainLocalPos.z));
var playerterrainNormal = terrain.terrainData.GetInterpolatedNormal(playernormalizedPos.x, playernormalizedPos.y);
newtree.position = new Vector3(newtreenormalizedPos.x , 0 ,newtreenormalizedPos.y );
// 4 - Add tree to the terrain
treeList.Add(newtree);
Terrain.activeTerrain.terrainData.treeInstances = treeList.ToArray();
// 5 - Remove unwanted trees
var numTrees = treeList.Count;
var newtreeList : List.< TreeInstance > = new List.< TreeInstance >();
for ( var t : int = 0; t < numTrees; t ++ ) {
var vecreal = Vector3.Scale(terrain.terrainData.treeInstances[t].position,terrain.terrainData.size) + terrain.transform.position;
var distance = Vector3.Distance (player.transform.position, vecreal);
if(distance<80)
{
newtreeList.Add(terrain.terrainData.treeInstances[t]);
}
if (false)
Debug.Log("Existing Tree (XZ): " + treeList[t].position.x + " " + treeList[t].position.z + " Y: " + treeList[t].position.y + " / " + vecreal.x + " " + vecreal.z);
}
// Overwrite terrain trees with list trees
Terrain.activeTerrain.terrainData.treeInstances = newtreeList.ToArray();
treeList = newtreeList;
// 6 - KLUDGE: Force colliders to work
//terrain.terrainData.SetHeights(0, 0, new float[0, 0]);
//var heights: float[,] = terrain.terrainData.GetHeights(0, 0, 0, 0);
//terrain.terrainData.SetHeights(0, 0, heights);
// 7 - Log
Debug.Log ("Player terrainPos: " + player.position + " normalizedPos: " + playernormalizedPos + " terrainNormal: " + playerterrainNormal + " numTrees: " + Terrain.activeTerrain.terrainData.treeInstances.length );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment