I am making a game in unity that allows you to destroy terrain, The problem I am having is that the farther away that I get from the origin (0,0), The more inaccurate the point where the destruction occurs is. Is there a way to make it more accurate?
// Use this for initialization
void Start ()
{
//The width and height of the heightMap
xRes = TerrainMain.terrainData.heightmapWidth;
yRes = TerrainMain.terrainData.heightmapHeight;
//Getting the heightMap points
saved = TerrainMain.terrainData.GetHeights(0, 0, xRes, yRes);
heights = TerrainMain.terrainData.GetHeights(0, 0, xRes, yRes);
}
void TerrainDestruction()
{
Vector3 actualPosition;
actualPosition.x = MyPosition.x;
actualPosition.y = MyPosition.y;
actualPosition.z = MyPosition.z;
//Getting the x and y position of the player
int xPosition = (int)(actualPosition.x);
int zPosition = (int)(actualPosition.z);
Debug.Log("x: " + xPosition);
Debug.Log("z: " + zPosition);
heights[zPosition, xPosition] -= 0.001f;
TerrainMain.terrainData.SetHeights(0, 0, heights);
TerrainMain.terrainData.SetHeights(0, 0, heights);
}
void OnApplicationQuit()
{
TerrainMain.terrainData.SetHeights(0, 0, saved);
}
// Update is called once per frame
void Update ()
{
//Getting the actual Position of the player with respect to the terrain
MyPosition = MyMouse.mousePosition;
if (Input.GetMouseButtonDown(0))
{
TerrainDestruction();
}
}