Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

[Solved] Placing trees with scripts on terrain

Discussion in 'Editor & General Support' started by BondoGames, Mar 25, 2017.

  1. BondoGames

    BondoGames

    Joined:
    Dec 7, 2013
    Posts:
    408
    It's a pretty simple survival game, you go up to trees and it adds to your wood count. But I can't be expected to manually place the 20,000 trees I have on the map (via mass tree placement terrain tool). And if I use that tool the scripts on the trees don't work. So I'm really confused on what it is I should do, and I certainly don't have any money to spend on an editor extension. Thank you.

    EDIT:



    As you can see I've already done the terrain to how I'd want, and as you can clearly see it'd be too much work to place trees down one by own

    EDIT:
    Discovered a solution. It is to grab all the TreeInstances in the terrain's TerrainData and place prefabs. Then remove all of the trees from the TerrainData. Word of caution (As I've just found out the hard way) changing TerrainData in-game effects the TerrainData in the editor. So when I stopped playing the game all the trees were gone in the editor as well. But it still works.

    (In short it removes all trees on the map and replaces them with the GameObject you set, however the trees will also be removed from the map on the editor so use this carefully)

    Here's the code I developed:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StartGame : MonoBehaviour {
    6.  
    7.     public GameObject theTree;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         // Grab the island's terrain data
    12.         TerrainData theIsland;
    13.         theIsland = GameObject.Find ("PrimeIsland").GetComponent<Terrain> ().terrainData;
    14.         // For every tree on the island
    15.         foreach (TreeInstance tree in theIsland.treeInstances) {
    16.             // Find its local position scaled by the terrain size (to find the real world position)
    17.             Vector3 worldTreePos = Vector3.Scale(tree.position, theIsland.size) + Terrain.activeTerrain.transform.position;
    18.             Instantiate (theTree, worldTreePos, Quaternion.identity); // Create a prefab tree on its pos
    19.         }
    20.         // Then delete all trees on the island
    21.         List<TreeInstance> newTrees = new List<TreeInstance>(0);
    22.         theIsland.treeInstances = newTrees.ToArray ();
    23.     }
    24. }
    25.  
     
    Last edited: Apr 2, 2017
    communityusername likes this.
  2. BondoGames

    BondoGames

    Joined:
    Dec 7, 2013
    Posts:
    408
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    5,598
  4. BondoGames

    BondoGames

    Joined:
    Dec 7, 2013
    Posts:
    408
    How would I go about seeing which tree I've interacted with from the tree instances array?
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    5,598
    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
     
  6. BondoGames

    BondoGames

    Joined:
    Dec 7, 2013
    Posts:
    408
    Are there any alternatives to this? I still want everything that comes along with a unity tree (Billboard at distance, easy placement) but all the features of a prefab. I think there may be some extensions on the store but I have no cash to pay for that sort of thing
     
  7. BondoGames

    BondoGames

    Joined:
    Dec 7, 2013
    Posts:
    408
    Like, how does The Forest do it? They have very interactive trees and they seem to hold all the features of a normal Unity tree
     
  8. BondoGames

    BondoGames

    Joined:
    Dec 7, 2013
    Posts:
    408
  9. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    5,598
    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)
     
  10. BondoGames

    BondoGames

    Joined:
    Dec 7, 2013
    Posts:
    408
    Alright, how would I replace them? I hate to ask so much but there is so little information on this
     
  11. BondoGames

    BondoGames

    Joined:
    Dec 7, 2013
    Posts:
    408
  12. BondoGames

    BondoGames

    Joined:
    Dec 7, 2013
    Posts:
    408
    Fixed it - fix posted in thread
     
  13. Koala_Squad

    Koala_Squad

    Joined:
    Aug 30, 2017
    Posts:
    9
    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.
     
  14. BondoGames

    BondoGames

    Joined:
    Dec 7, 2013
    Posts:
    408
    Sure, and I'd love to see the final version. Message me with where I can check out development!
     
  15. Will-D-Ramsey

    Will-D-Ramsey

    Joined:
    Dec 9, 2012
    Posts:
    221
    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.
     
  16. egartnuc

    egartnuc

    Joined:
    Jun 2, 2018
    Posts:
    23
    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.