Terrain Height map World Position

So in the screen you see i have aligned some sprite objects to the terrain, now in this bit of code i try to modify the terrain heights based on position of my guide dots.

        var TD = Map.terrainData;
        var W = TD.heightmapWidth;
        var H = TD.heightmapHeight;
        Debug.Log(W);
        Debug.Log(H);
        var TH = TD.GetHeights(0, 0, W, H);

        for (int i = 0; i < TilePositions.Count; i++) // I think that all the positions exist so therefor :
        {
            int X = (int)TilePositions[i].x;
            int Z = (int)TilePositions[i].z;
           
            if (Random.Range(0, 4) == 1) // Just basic randomization of height
            {
                TH[X, Z] = 0.001f;
            }
            else
                TH[X, Z] = 0; // One of these XZ does not exist
        }
        TD.SetHeights(0, 0, TH);

I not sure i understand what exactly i am doing, or waht i am missing. But it does work for some coordinates and then stops working before covering the map.

If you can help thank you

This stuff can get hairy because there are so many coordinate systems involved: world, local, terrain position, terrain normalized height, heightmap resolution, etc.

This script does it all correctly and contains decent commentary, so perhaps it can be of guidance to you:

1 Like

Is that a from raycast?

I’m not sure I understand that question. All the code is linked in the comments on the video, feel free to look around and see what it does. It’s pretty straightforward and well-commented.