Search Unity

edit terrain from c# a problem of accuracy

Discussion in 'Scripting' started by MaorDany, Jun 16, 2018.

  1. MaorDany

    MaorDany

    Joined:
    Dec 31, 2017
    Posts:
    105
    The code works well but is not accurate. I manage to lift it up but the location is not always near a mouse click.
    Very close but not accurate. Why Why Why :(

    Code (CSharp):
    1. if (Input.GetMouseButton(0))
    2.         {
    3.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.             RaycastHit hit;
    5.  
    6.             if (Physics.Raycast(ray, out hit))
    7.             {
    8.                 float[,] heights = terrainMain.terrainData.GetHeights(0, 0, terrainMain.terrainData.heightmapWidth, terrainMain.terrainData.heightmapHeight);
    9.                 heights[(int)hit.point.z, (int)hit.point.x] = 100f;
    10.                 terrainMain.terrainData.SetHeights(0, 0, heights);
    11.             }
    12.         }
     
  2. MaorDany

    MaorDany

    Joined:
    Dec 31, 2017
    Posts:
    105
    I see when I press a part that comes out (
    Code (CSharp):
    1. heights[(int)hit.point.z, (int)hit.point.x] = 100f;
    ) The mouse click position is even less accurate.

    Is it possible to add Plane Then identify the location of the click on Plane And according to the point data edit the Terrain?