Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug terrain trees y coordinate not working

Discussion in 'World Building' started by Sevardos, Apr 18, 2021.

  1. Sevardos

    Sevardos

    Joined:
    Jan 12, 2018
    Posts:
    8
    Hi,

    I have encountered what seems to be a strange bug, or maybe I am doing something wrong....

    I have a terrain, with a heightmap and some textures on it. Then I want to add trees to it, which for test purposes looks basically like this:

    Code (CSharp):
    1. trees = new TreeInstance[nrTrees];
    2. #looping over nrTrees, position depends on the counter and just creates a grid with y coordinate 0f.
    3.  trees[treeCounter] = createTreeInstance(position);
    4. #loop ends
    5. terrain.terrainData.SetTreeInstances(trees, true);
    6.  
    7. private TreeInstance createTreeInstance(Vector3 position)
    8.     {
    9.         TreeInstance newInstance = new TreeInstance();      
    10.         newInstance.position = position;
    11.         newInstance.heightScale = .3f;
    12.         newInstance.widthScale = .3f;
    13.         newInstance.prototypeIndex = 0;
    14.         newInstance.lightmapColor = Color.white;
    15.         newInstance.color = Color.white;
    16.         return newInstance;
    17.     }
    This has the result that the trees appear on a grid as they are supposed to do, but all of them are drawn at a height of zero (terrain height, not world height), both in the game window and in the scene window. If I close either window and reopen it from the editor then they appear at the correct height in the window that was reopened. So reopening only one of them means the game window and scene window show them at different positions. You can see this at the images below. The upper one is how it looks after I add the trees. The lower one is after I closed and reopened the game window without doing anything else.
    If I change the heightmap of the terrain and move the terrain without any change to the trees, they are at the wrong position again, not at a height of zero this time, I assume its just the same height as they had been before the change in the heightmap. Again, closing and opening snaps them back to terrain height, but only for the window that has been closed and reopened.
    Compiling everything means the position is always zero since I cant reopen the game window there.

    Anyone knows whats going on there? Is that a known problem, a known cause, is there a workaround?

    unity_bug0.PNG unity_bug1.PNG
     
  2. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    566
    Run this at the end of your function:

    Code (CSharp):
    1. terrain.terrainData.SetTreeInstances(terrain.terrainData.treeInstances, true);
    2. terrain.Flush();
    3.  
     
    Last edited: Apr 19, 2021
    Sevardos likes this.
  3. Sevardos

    Sevardos

    Joined:
    Jan 12, 2018
    Posts:
    8
    That works, thanks.
     
    Mortalanimal likes this.