# Going from 0 to 1 and then back to 0 within a time frame

I can’t seem to find a simple example of this anywhere. I don’t have much knowledge regarding sine waves but I’m more than certain that’s what I need to use for this. I’ve also tried Mathf.PingPong but I can’t seem to get that working correctly either. I honestly don’t even know what to do to do this without using some crappy if (value > max) go backwards etc.

There must be an equation, that when passed an elapsed time, can go from min to max then back to min once the elapsed time reaches the desired duration.

``````For example with a duration of 1 second:

Elapsed Time of 0
return value = 0

Elapsed Time of 0.5
return value = 1

Elapsed Time of 1
return value = 0
``````

float duration = 1 ; // In seconds
float maxValue = 1 ;
float elapsedTime = Time.time - startTime ; // startTime can be equal to 0
float output = Mathf.PingPong( 2 * elapsedTime / duration, maxValue );

OR, for a smoother ping pong

``````float duration = 1 ; // In seconds
float maxValue = 1 ;
float elapsedTime = Time.time - startTime ; // startTime can be equal to 0
float output = elapsedTime * Mathf.PI / duration ;
output = Mathf.Cos( output + Mathf.PI ) + 1 ;
output *= maxValue / 2 ;
``````

Desmos : Graphing Calculator