The quick brown fox jumps over the lazy dog 2

The quick brown fox jumps over the lazy dog

8 Likes

The quick brown fox jumps over the lazy dog

some info here,

If you paint trees on terrain, then you need to access them through terraindata,

The quick brown fox jumps over the lazy dog

take closest tree to player,
or if you create separate colliders for each tree position,
then could assign tree index number for each collider…(to keep track in script).

but in that case, shouldnt remove trees, just replace with another object (maybe tree stump),
so that treecount and indexes wont change… *havent tested if would work actually

The quick brown fox jumps over the lazy dog

The quick brown fox jumps over the lazy dog

The quick brown fox jumps over the lazy dog

1 Like

You could still paint trees with terrain tools, then take their positions and replace terrain trees with prefab trees.
Can use LODs on tree models to improve performance.

Theres also mesh terrain alternatives, most likely those are more flexible… (not sure if any free ones)

The quick brown fox jumps over the lazy dog

1 Like

The quick brown fox jumps over the lazy dog

The quick brown fox jumps over the lazy dog

Cool Solution! I have a flamethrower in my game, and I wanted to be able to set trees on fire with it. Can I use your code? I’ll give credit.

The quick brown fox jumps over the lazy dog

Ive been working on systems to make trees more interactive Ex. chop-able, burnable, etc for over a year and it looks like I handled it pretty similar to this forum. I used to have a kinda large script for handling chopping a tree down but im in the process of reworking my tree chopping system to be more streamline.

I’d love to hear more.

I’m currently working on tree chopping, as well. And every other post I find on the topic directs me to this post (http://answers.unity3d.com/questions/650308/how-do-i-interact-with-terrain-trees.html) but it doesn’t deal with the collider (a.k.a invisible higher terrain where the tree was).

The only “collider fix” I found runs through the entire tree array upon interaction causing temporary freeze of the game.

Hi all, not sure who might find it useful but I made an improved version of this code that takes into account the terrain trees’ position and rotation, as well as putting them under a parent object so they can be managed more easily:

void ConvertTerrainTreesToPrefabs()
    {
        // Grab the island's terrain data
        TerrainData thisTerrain;
        thisTerrain = GetComponent<Terrain>().terrainData;

        //GameObject to be the parent
        GameObject treeParent =  new GameObject("treeParent");

        // For every terrain tree on the island
        foreach (TreeInstance terrainTree in thisTerrain.treeInstances)
        {
            // Find its local position scaled by the terrain size (to find the real world position)
            Vector3 worldTreePos = Vector3.Scale(terrainTree.position, thisTerrain.size) + Terrain.activeTerrain.transform.position;
           
            // Create the new tree out of the prefab
            GameObject prefabTree = Instantiate(theTree, worldTreePos, Quaternion.identity, treeParent.transform); // Create a prefab tree on its pos

            //Then set the new tree to the randomized size and rotation of the terrain tree
            prefabTree.transform.localScale = new Vector3(terrainTree.widthScale * 50, terrainTree.heightScale * 50, terrainTree.widthScale * 50);
            prefabTree.transform.rotation = Quaternion.AngleAxis(terrainTree.rotation * 57.2958f, Vector3.up);
        }
        // Then delete all the terrain trees on the island
        List<TreeInstance> newTrees = new List<TreeInstance>(0);
        thisTerrain.treeInstances = newTrees.ToArray();
    }
2 Likes

Why delete all the trees when you can just set tree distance to 0 on the terrain and save them incase you need to edit them? In theory you can do the prefab swap at runtime as well, then set tree distance to 0.

1 Like

Made something like this:

using System.Collections.Generic;
using UnityEngine;
public class TreeGenerator : MonoBehaviour
{
    [SerializeField] private bool createOnStart;
    [SerializeField] private List<GameObject> trees;
    [SerializeField] private float minSize,maxSize;
    private void Start()
    {
        if (!createOnStart) return;
        var terrain = GetComponentInChildren<Terrain>();
        var terrainData = terrain.terrainData;
        foreach (var terrainTree in terrainData.treeInstances)
        {
            var worldTreePos = Vector3.Scale(terrainTree.position, terrainData.size) + Terrain.activeTerrain.transform.position;
            var tree = Instantiate(trees[Random.Range(0,trees.Count)], worldTreePos, Quaternion.identity, transform); 
            tree.transform.localScale = Vector3.one*Random.Range(minSize,maxSize);
            tree.transform.rotation = Quaternion.AngleAxis(Random.Range(-360f,360f), Vector3.up);
        }
        terrain.treeDistance = 0;
    }
}

use guys)

1 Like

Good stuff, thank you, and all the other contributors. Tested, working effortlessly.