Im trying to animate the rotor of a wind generator in code. But im trying to randomize the speed of the rotation to simulate a wind generator reacting to varying wind speeds. So one moment the blades might barely be spinning, next moment they might be spinning at high speed. and everywhere inbetween.
Iāve searched as best i can but i have not found an answer.
I have tried invoke repeating and random ranges but i cannot seem to get this working as desired.
The problem with using a random range between 0 and 200 is that itāll very quickly average to a value of 100 so the rotor will appear to move at a constant speed.
Youāre better of using something like perlin noise which will give you much nicer random curves rather than the white noise youād get from the random.range function. (Unity - Scripting API: Mathf.PerlinNoise)
I would generate Bezier curves, and lerp through those
The Bezier curve points would obey contraints, that can be changed at runtime to make the wind more volatile/calm, or keep it within certain ranges.
You can use random range for the points (using the contraints)
You could use invoke to generate the beziers every x seconds and then take x seconds to lerp through a Bezier, but itās even better if the speed of lerping through a bezier curve is also variable, because that gives you a much greater control over volatility.
In that case you wouldnāt use invoke.
Also, I would apply this to a windzone (controlling windzones requires reflection, but the scripts can be found on this forum), not to the rotor. I would then fetch the data from the windzone and apply it to the rotor indirectly.
That way the wind can be applied to particles/trees.
Perlin and Bezier are definitely nice ways of doing it, I was thinking more like setting a velocity at an interval and lerping between them:
public class WindGenRotator
{
public bool enabled = true;
float targetVelocity = 1f;
float velocity = 1f;
float rotation = 0f;
public float maxVelocity = 4f;
public float minVelocity = 0.1f;
public float maxVelUpdateInterval = 3f;
public float minVelUpdateInterval = 1f;
void Start()
{
StartCoroutine(VelocityUpdate);
}
void Update()
{
velocity += (targetVelocity - velocity) * Time.deltaTime;
transform.Rotate(transform.forward, velocity * Time.deltaTime);
}
IEnumerator VelocityUpdate()
{
while(enabled)
{
targetVelocity = Random.Range(minVelocity, maxVelocity);
yield return new WaitForSeconds(Random.Range(minVelUpdateInterval, maxVelUpdateInterval));
}
}
}
Havenāt tested it, wrote it all in the post editor, but I think the general gist of it is pretty clear. The min/max update interval fields are for how frequently the velocity is changed.
Thanks Scabbage. Your code works nicely as does jschieckās. Iām learning a lot here! Impressed you wrote that code without the editor, it was spot on except for a couple of parentheses on the coroutine call Lol. Seriously thatās awesome tho
Thanks tonemcbride, yep my code just had the rotor jumping to various states of rotation. Thanks for the idea regarding Perlin noise for the randomness itās all good learning as an artist going into scripting.
Na ra Ku(updated: Hikiko66)
. I really like this idea. I found having multiples wind generators running the above code the randomness ruined the illusion of having wind blowing thru when one wind generator spins fast and the other slow. Your idea sounds like it would fix this. Iām such a noob, but Iāll give it some serious effort.