Smooth Random.Range

I would like to Smooth the Random.Range function so the numbers gradually change.

Is there any way to do this using a Mathf.Lerp function?

This is for creating lightning in the Linerender component that gradually shifts from one random to another.


FPSyndicate

Ah, what you’re looking for is the Perlin noise function. If only you’d had that notion about 15 years ago, it could have been you accepting the Academy Award for Technical Excellence! You can find a C# class implementation in the “Procedural Examples”.

There are 1D, 2D and 3D versions. Basically, you use some smoothly varying input (like Mathf.Sin), and it gives you a smoothly varying random output. A key feature is that for a given input, you always get the same output.

This is a simple solution, hope it may help you. Swings the number smooth between 0 and 1 at a rate defined by slope, and changes swing direction at random intervals defined by interval or at the limits 0 and 1. Adjust slope and interval to get the desired results.

var slope:float = 0.5;
var interval:float = 1;
private var smooth:float = 0.5;
private var tNext:float = 0;
private var dir:float = 1;

function Update(){

	if (Time.time>tNext){
		tNext += interval*(0.5+Random.value);
		dir = -dir;
	}
	smooth += dir*slope*Time.deltaTime;
	if (smooth>1 || smooth<0) dir = -dir;
	smooth = Mathf.Clamp(smooth,0,1);
	// Example: using smooth to vary amb light
	RenderSettings.ambientLight = Color.white*smooth;
}