I’m trying to ramp up the speed of a fan from 0 to 1.0 animation speed. I have a script that begins the animation playback and I thought using a Mathf.SmoothDamp would do it, but it just gives me really slow playback. Script is below - anyone have any thoughts as to another way of doing this?
(for clarity, I’ve omitted the code for the StopFan() function, since it doesn’t affect this issue)
var fanStart : float = 0.0;
var fanEnd : float = 5.0;
var smoothTime : float = 2.0;
var velChange : float = 0.0;
private var clicked : boolean = false;
private var switchMe : GameObject;
switchMe = GameObject.Find("finalSwitch");
private var fan : GameObject;
fan = GameObject.Find("fanPivot");
private var fpsCharacter : GameObject;
fpsCharacter = GameObject.Find("Graphics");
function OnMouseUp()
{
if(clicked)
{
StopFan();
}
else if (!clicked)
{
StartFan();
}
}
function StartFan()
{
animation["switchedOn"].speed = 1.0;
animation.Play();
audio.Play();
var rampUpSpeed : float = Mathf.SmoothDamp(fanStart,fanEnd,velChange,smoothTime);
fan.animation["fanRotation"].speed = rampUpSpeed;
fan.animation.Play();
fan.audio.Play();
clicked = true;
}
Your fanStart value isn’t being updated to the current speed value (the result of the lerp/smooth damp so your constant value for T just returns the same value during each iteration.
// C#
float currentSpeed = 0f;
float finalSpeed = 1f;
void Update () {
animation["fanRotation"].speed = currentSpeed;
}
IEnumerator RampFanSpeed () {
while (currentSpeed < finalSpeed) {
currentSpeed = Mathf.Lerp(currentSpeed, finalSpeed, 0.25f);
}
}
You must create a routine with a loop that counts time from 0 to the desired ramp duration in seconds, and use Mathf.Lerp(fanStart, fanEnd, time/duration) to calculate the fan speed at each iteration.
To let Unity work while you’re counting time, insert a simple yield in the loop, what makes the function a coroutine. Coroutines run in the background: they stop at the yield instruction and let Unity free till the next update cycle, when the coroutine execution is automatically resumed right after the yield.
function StartFan(){
animation["switchedOn"].speed = 1.0;
animation.Play();
audio.Play();
RampUpSpeed(10.0); // start the coroutine RampUpSpeed with 10 seconds ramp duration
fan.animation.Play();
fan.audio.Play();
clicked = true;
}
function RampUpSpeed(duration: float){
var t: float = 0;
while (t < duration){ // loop during "duration" seconds:
t += Time.deltaTime; // count time...
// and set speed from fanStart to fanEnd proportionally to time/duration:
fan.animation["fanRotation"].speed = Mathf.Lerp(fanStart, fanEnd, t/duration);
yield; // let Unity free till next update cycle
}
}
NOTE: If the fan sound is constant and loopable, you can even set its frequency proportional to the speed - just add the line below right before the yield instruction:
fan.audio.pitch = t/duration;