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

Problem with SetHeights()

Discussion in 'Scripting' started by egartnuc, Nov 13, 2018.

  1. egartnuc

    egartnuc

    Joined:
    Jun 2, 2018
    Posts:
    23
    I gotta be honest. I'm way over my head here. And neither the documentation nor google seem to be able to help me further.

    I'm trying to remove the terrain tree collider of a tree that I chopped upon runtime. The tree removes fine as I want it, but the collider I cannot manage to remove.

    The code I keep stumbling upon is the following:

    Code (CSharp):
    1.             float[,] heights = Terrain.activeTerrain.terrainData.GetHeights(0, 0, 0, 0);
    2.             Terrain.activeTerrain.terrainData.SetHeights(0, 0, heights);
    I can't seem to get it to work.

    I tried the following things:
    - Change SetHeights to SetHeightsDelayLod
    - Added ApplyDelayedHeightmapModification() after the above code
    - Enabling/disabling the terrain collider
    - All of the above in a coroutine with short delays.

    I appreciate any help. Thanks.

    BR
    Tomas
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,547
    The GetHeights() and SetHeights() calls only adjusts the vertical height of the ground, nothing to do with the colliders.

    I'm not sure how tree colliders are done in the terrain. Maybe the colliders are getting baked in? Try marking the tree geometry as not static...

    Alternately go into the scene and try to select the collider, see what GameObject it is hanging on. It might not be possible, but it's a thing to try.
     
  3. egartnuc

    egartnuc

    Joined:
    Jun 2, 2018
    Posts:
    23
    @Kurt-Dekker thank you for your reply. You are correct, that was bad phrasing of the problem. The terrain collider is 1 collider only and it follows the mesh of it. Trees are part of this same collider. The problem is when by script removing a tree instance, the terrain collider needs adjusting, and setheight solves this problem (at least according to numerous threads and discussions I read, I just havent found a solution yet that works).

    I was not aware of the tree geometry setting, will look for it tonight, thank you.

    Lastly since this is terrain trees they all, along with the terrain, is just one gameobject. So there is no single collider to be worked with in a regular GO kinda way. This is for performance reasons, and considering the amount of trees I will have active at any given point in time, the GO alternative for trees just isnt a viable one.
     
  4. egartnuc

    egartnuc

    Joined:
    Jun 2, 2018
    Posts:
    23
    So after a few more hours of googling on semi-related subjects I found threads on a bug from 2012. The bug at hand causes the collider to not update properly even after being manipulated through script until the Unity client is restarted. I thought 'Hey, a longshot but whatever...'

    So I made a stack of trees, chopped them all with the above code in script. I confirmed that they were still there afterwards (the colliders). Then I saved, restarted the client, and lo' and behold: the old tree colliders (or rather pointy mesh shapes that made terrain collider be in the way) are gone.

    I don't know if I ran into this bug, or if it's anmother reason, but yeah. Now I just need to find a way around that.

    Any suggestions to keep working the problem are greatly appreciated.


    BR
    Tomas