Here is an example for Moving on the X axix
float _sidewaysMovementTime = 1f;
IEnumerator
MoveSideways(bool left)
{
float currentTime = 0;
float startX = transform.position.x;
float targetX = transform.position.x + ((left ? -1 : 1) * 0.3f);
while (currentTime < _sidewaysMovementTime)
{
currentTime += Time.deltaTime;
transform.position = new Vector3(
Mathf.Lerp(startX, targetX,
currentTime / _sidewaysMovementTime),
transform.position.y,
transform.position.z);
yield return null;
}
}
Here is an example of audio Fade In and Out using LERP.
IEnumerator
FadeIn()
{
float currentTime = 0;
while (currentTime < FadeDuration)
{
currentTime += Time.deltaTime;
_audioSourceBackgroundMusic.volume =
Mathf.Lerp(0, _startingMusicVolume, currentTime / FadeDuration);
yield return null;
}
}
IEnumerator
FadeOut()
{
float currentTime = 0;
float startingVolume = _audioSourceBackgroundMusic.volume;
while (currentTime < FadeDuration)
{
currentTime += Time.deltaTime;
_audioSourceBackgroundMusic.volume =
Mathf.Lerp(startingVolume, 0, currentTime / FadeDuration);
yield return null;
}
}
Comments
Post a Comment