# Diamond Square Algorithm

Hello,

So I’ve been making a random terrain generator however the terrain it is producing is not excellent. I would like it to appear more random and realistic. Here is my code and a screenshot of what I’m getting.

``````#pragma strict
var width : int;
var height : int;
var desiredHeight : float;
var step: int;
var minNoise : float;
var maxNoise : float;
function Start () {
var terrainDat = GetComponent(Terrain).terrainData;
width = terrainDat.heightmapWidth;
height = terrainDat.heightmapHeight;
var xAnchor : int = 0;
var yAnchor : int = 0;
var heights = terrainDat.GetHeights(0,0,width,height);

width = width-1;
height = height-1;
step = width;
//corners
heights[xAnchor,yAnchor] = desiredHeight;
terrainDat.SetHeights(0,0,heights);
heights[xAnchor,height] = desiredHeight;
terrainDat.SetHeights(0,0,heights);
heights[width,yAnchor] = desiredHeight;
terrainDat.SetHeights(0,0,heights);
heights[width,height] = desiredHeight;
terrainDat.SetHeights(0,0,heights);

for(var p = 0; p<=1400; p++){
//print(width);
//diamond
heights[xAnchor+width/2,yAnchor+height/2] = (((terrainDat.GetHeight(xAnchor,yAnchor)+terrainDat.GetHeight(width,yAnchor)+terrainDat.GetHeight(xAnchor,height)+terrainDat.GetHeight(width,height))/4.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
terrainDat.SetHeights(0,0,heights);
//square
heights[xAnchor+width/2,yAnchor] = (((terrainDat.GetHeight(xAnchor,yAnchor)+terrainDat.GetHeight(width,yAnchor))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
terrainDat.SetHeights(0,0,heights);
heights[xAnchor,yAnchor+height/2] = (((terrainDat.GetHeight(xAnchor,yAnchor)+terrainDat.GetHeight(xAnchor,height))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
terrainDat.SetHeights(0,0,heights);
heights[xAnchor+width/2,height] = (((terrainDat.GetHeight(width,height)+terrainDat.GetHeight(width,yAnchor))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
terrainDat.SetHeights(0,0,heights);
heights[width,yAnchor+height/2] = (((terrainDat.GetHeight(width,height)+terrainDat.GetHeight(xAnchor,height))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
terrainDat.SetHeights(0,0,heights);
//check next
if(xAnchor+width >= 64 && yAnchor+height >=64){
//print("y");
width = width/2;
height = height/2;
step = width;
xAnchor=0;
yAnchor=0;
}
else{
//print("n");

if(xAnchor+width >=64){
yAnchor = yAnchor + height;
xAnchor = 0;
}
else{xAnchor = xAnchor + width;}
}
}
}
``````

Well yes how can I change my code to make it more realistic?