Search Unity

How to chop tree down in unity terrain

Discussion in 'Scripting' started by Zultron, Sep 30, 2013.

  1. Zultron

    Zultron

    Joined:
    Dec 26, 2012
    Posts:
    59
    I am trying to get my head around one problem I am having. I wanna be able to chop down a tree however this means I cannot use terrain tree painter since there is no way as far I am aware to identify that particular tree. So does that mean the only way around this is to create a prefab for separate tree and place one by one in the world? If anyone has idea of how to implement such feature direct me in right direction.. Plus on top of that tree has to grow back .

    Any help and guidance appreciated
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,442
  3. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Creating a prefab is a good way, but I have seen topics about going the terrain data direction. Did you find anything on this in the search, cause I think there is a free package to download around here also.
     
  4. Zultron

    Zultron

    Joined:
    Dec 26, 2012
    Posts:
    59
    Last edited: Sep 30, 2013
  5. Zultron

    Zultron

    Joined:
    Dec 26, 2012
    Posts:
    59
    Its just dont seem right to use prefabs for something likes this when I will have 1000s of trees and vegitation that will be gatherable. First time I am hearing terrain data direction... I shall take a look at that subject

    P.S. Let me know the package name I check it out
     
    Last edited: Sep 30, 2013
  6. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    terrain.terrainData contains a property called treeInstances which describes positions and other properties of all the trees planted on the terrain. When a player chops a tree, you can find the nearest tree in treeInstances array and remove it. If I remember correctly, the positions in the array are normalized, e.g. (0;0) corresponds to one corner of the terrain and (1;1) to the opposite corner.

    PS
    You may find it useful to place all the tree colliders as separate game objects on the scene, while using the terrain engine just for visuals. This way has two advantages:
    1) It's easier to determine whether a player have collided with a tree or maybe it's something else.
    2) The terrain collider will lag less when you update it. It will not have to deal with all the tree colliders.
     
    Last edited: Sep 30, 2013
  7. Zultron

    Zultron

    Joined:
    Dec 26, 2012
    Posts:
    59

    You know I literally just came up with solution as you said Terrain data stores trees into array which means I can identify those tress since they stored in terrain data, when they are in area therefore i can destroy them, I will prototype this tomorrow should be interesting. This means in theory I can gather all the flowers and grass ect.
     
    Last edited: Sep 30, 2013
  8. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    You can do pretty anything, add/remove trees, grass, change textures, rise or lower terrain. The main problem is that the engine often lags if the map resolutions are high, like 512x512 or bigger (height map, splat map, etc).
     
  9. Zultron

    Zultron

    Joined:
    Dec 26, 2012
    Posts:
    59
    Can you elaborate on that.. Do you mean creating trees seperate as prefabs and placing them on scene or you mean utilising terrain with trees and then somehow place coliders separate?
     
  10. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    The second. In "Terrain Collider" options turn off "Create Tree Colliders" and for each tree procedurally create and place on the scene a separate game object - a collider. There's actually a third advantage: you can use colliders of any type, not just capsule ones as the terrain engine requires.
     
    Last edited: Oct 1, 2013