Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Removing grass doesn t really work

Discussion in 'World Building' started by Vire7777, Mar 6, 2021.

  1. Vire7777

    Vire7777

    Joined:
    Mar 13, 2019
    Posts:
    20
    Hello people

    I have a map with terrain and grass
    When i add a building on it, i would love the grass to disappear under (because else, the grass appears in the building :p)

    So i implemented this

    Code (CSharp):
    1. private void CutGrass(Vector3 buildingCenter, int size)
    2.     {
    3.         Vector3 terrainPos = terrainManager.ConvertToSplatMapCoordinate(buildingCenter);
    4.  
    5.         int[,] map = terrain.terrainData.GetDetailLayer((int)terrainPos.x - size / 2, (int)terrainPos.z - size / 2,
    6.           size, size, 0);
    7.  
    8.         // For each pixel in the detail map...
    9.         for (int z = 0; z < size; z++)
    10.         {
    11.             for (int x = 0; x < size; x++)
    12.             {
    13.                 map[x, z] = 0;
    14.             }
    15.         }
    16.  
    17.         // Get all of layer zero.
    18.         for (int i = 0; i < terrain.terrainData.terrainLayers.Length; i++)
    19.         {
    20.             // Assign the modified map back.
    21.             terrain.terrainData.SetDetailLayer((int)terrainPos.x - size / 2, (int)terrainPos.z - size / 2, i, map);
    22.         }
    23.     }
    This code works quite well
    Like this, whatever are the layers under the building, the grass disappears... until...
    until the grass index is too high

    When i m creating my grass at the beginning, it seems that if i have more grass initial objects than the number of layers, the other ones are not disappearing

    For example
    If i have 3 layers of ground (rock, dirt and green)
    and i have 5 type of grass (generally these are png that i add as billboard a bit everywhere)
    and then i put a building somewhere
    Only the 3 first type of grass under that building would be removed (it means, every instance of these 3 types would be removed which is good, but not the 2 others)
    the 2 next, they stay

    AND THAT IS ANNOYING :p

    So if you have any idea how to remove ALL the type of grass, i would be really pleased :)
     
  2. warthos3399

    warthos3399

    Joined:
    May 11, 2019
    Posts:
    977
    I hope i dont sound rude...why are you wasting your time writing a script....delete the grass thats under the buiilding?
     
  3. Vire7777

    Vire7777

    Joined:
    Mar 13, 2019
    Posts:
    20
    because how could i do that during runtime ???
    I can t go to every people playing the game and say, hey hey i ll cut the grass, wait a minute
     
  4. warthos3399

    warthos3399

    Joined:
    May 11, 2019
    Posts:
    977
    Sorry, now i understand what you are saying, you have set up layers for certain objects, and your trying to exclude all 8 of them. Ill replicate this with your code when i get a chance...
     
unityunity