Convert player world position into terrain detail layer coordinates (GetDetailLayer)

I’ve found one or two answers on this but didn’t fully understand them. All i have is a player on top of the terrain and i just want to remove some details/grass if the player is on that position in the terrain. I’m currently using GetDetailLayer to get the detail information of the terrain (returns a int map[x,y]). What i need is to convert the player world position into this X, Y coordinate in the terrain and to finally set map[x,y] = 0 to eliminate the detail/grass. Any help?

I got it to work :slight_smile: I basically used this: (from some other answer)

private Vector3 ConvertWordCor2TerrCor(Vector3 wordCor)
    {
        Vector3 vecRet = new Vector3();
        Terrain ter = Terrain.activeTerrain;
        Vector3 terPosition = ter.transform.position;
        vecRet.x = ((wordCor.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth;
        vecRet.z = ((wordCor.z - terPosition.z) / ter.terrainData.size.z) * ter.terrainData.alphamapHeight;
        return vecRet;
    }

and then, to test, you need to clear some good amount of area around the position in the terrain to see results visually (because clearing only one pixel won’t make a difference visually if you have tons of grass), and if the grass is nicely dense in the map then you’ll see even more difference. I implemented this to cover the area based on the position obtained from the first block of code:

private void coverArea(int x, int y, int range) {
        int xmin = x - range;
        int ymin = y - range;
        int xmax = x + range;
        int ymax = y + range;
        
        for (int i = xmin; i <= xmax; i++) {
            for (int j = ymin; j <= ymax; j++) {
                map[j, i] = 0;
            }
        }
        t.terrainData.SetDetailLayer(0, 0, 0, map);
    }

This cover area doesn’t check boundaries (when the player is in the corner) so it will try to cover some out of index coordinates, but it will only throw an error and won’t crash your game, eventually this should be checked as well.
Terrain settings were: Width 30, length 30, position x= -15 and y = -15 (so it goes from -15 -15 to +15 +15 (because terrain origin is on bottom left corner) and resolution was 512.

void OnDrawGizmos()
{//draws Gizmos on the terrain using terrain data-Rex
//use small terrain
Terrain terrain = GetComponent();
Vector3 terrainSize = terrain.terrainData.size;

    for (int i = 0; i < terrain.terrainData.heightmapResolution; i += 1) // use steps of more than 1 for smoother interaction in editor
    {
        for (int k = 0; k < terrain.terrainData.heightmapResolution; k += 1)
        {
            // not sure why the gizmo y-heights are scaled out from 0,0 (about 5X) when doing it this way.
            // Uncomment this and comment the second one to see:
            //Vector3 pivot = new Vector3(i, terrain.terrainData.GetHeight(i, k), k);

            // this way fixes it (pretty much)--WHY??
            Vector3 pivot = new Vector3(k, terrain.terrainData.GetHeight(k , i ), i);

            float x = pivot.x / (terrainSize.x);
            float z = pivot.z / (terrainSize.z );
            Vector3 interpolatedNormal = terrain.terrainData.GetInterpolatedNormal(x, z);
            
            GUI.color = Color.blue;
            pivot  = terrain.transform.position+ new Vector3(pivot.x / terrain.terrainData.heightmapResolution * terrainSize.x, pivot.y, pivot.z / terrain.terrainData.heightmapResolution * terrainSize.y);
            Gizmos.DrawSphere(pivot, 0.2f);
           Gizmos.color = Color.cyan;
            Gizmos.DrawRay(pivot, interpolatedNormal * rayScale);
        }
    }
}

Hi @Serge144

Can you please post the complete code doing this

Thank in advance!