- Home /
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
Answer by Hellium · Apr 14, 2018 at 04:41 PM
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 ;
It works! I substituted Time.time with my own elapsed time, also I moved the max value outside of the pingpong as a multiplier, with 1 as the length, and I seem to be getting the desired result.
Thank you very much! This is my working code:
public float duration = 1; // In seconds
public float maxValue = 1;
public Slider displayValue;
private void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.B))
{
StartCoroutine(Wave(duration));
}
}
IEnumerator Wave(float d)
{
var elapsedTime = 0f;
while (elapsedTime < d)
{
var value = $$anonymous$$athf.PingPong(2 * elapsedTime / duration, 1) * maxValue;
Debug.Log("Value: " + value + "\nElapsed Time: " + elapsedTime);
elapsedTime += Time.deltaTime;
if (displayValue)
displayValue.value = value;
yield return null;
}
}
Huh? Why "20"? Why dividing by PI? the angle should be a multiple of PI. This actually creates a proper sine wave that starts at 0 and exactly hits the whole numbers:
float duration = 1 ; // In seconds
float maxValue = 1 ;
float angle = Time.time * 2f * $$anonymous$$athf.PI / duration;
float output = maxValue * 0.5f* (1f-$$anonymous$$athf.Cos( angle ));
edit actually dividing by the "duration" ^^. Since duration and frequency are the reciprocals from each other. t = 1/f and f = 1/t
Well, I fixed my answer, must admit I am tired and I looked for the values by feeling...
No problem. Btw i created a desmos graph for pingpong. The actual implementation of $$anonymous$$athf.PingPong can be found in the reference repository. Repeat is directly above. Clamp can be found further up, though the implemenation should be obvious ^^.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Does Mathf.PingPong always have to start at (0,0,0)? 1 Answer
mathf.movetowards not returning right values 1 Answer
Mathf function on GetKey 1 Answer
Ping pong position using lerp 3 Answers