- Home /
Is there a better way to tilt terrain at runtime?
I'm using this code to tilt my terrain at runtime. It works pretty well except it's very slow. It has to iterate through each height point so it ends up taking 2-3 seconds. I'd love to find a way to shave even a little bit of time off.
function OnGUI ()
{
if (GUI.Button(Rect(200,10,50,30), "tilt"))
{
SlopeAngle (0.1);
}
}
function SlopeAngle (change : float)
{
var tData = terrain.GetHeights(0, 0, terrain.heightmapWidth, terrain.heightmapHeight);
// iterate through height points
for (i = 0; i < terrain.heightmapHeight; i++)
{
for (j = 0; j < terrain.heightmapWidth; j++)
{
// lowers terrain points at end, gradually decreasing to 0 as it reaches end
tData[i, j] -= ((change / terrain.heightmapWidth) * j);
}
}
terrain.SetHeights(0, 0, tData);
}
Comment
Answer by commodore · Sep 16, 2014 at 06:06 PM
I feel stupid. I had a Debug.Log line in there that was making it run super slow. I took it out and it's all good now.
Not sure if it changes much but I also changed tData[i, j] -= ((change / terrain.heightmapWidth) j); to this tData[i, j] -= gradHeight j; and put this just before the loop var gradHeight : float = change / terrain.heightmapWidth;