# Arbitrary starting value for Mathf.Sin? How to start it at 0? Now with video example! :)

Should I use another Mathf??

How would you “smoothly” trigger an oscillation of a variable to start at zero upon entering a trigger, then continue to oscillate [Mathf.Sin(Time.time)*x] until trigger exit, then smoothly return to zero, all at a definable rate?

===>video example

``````This is what I have so far but doesn't seem to work right , I'm a newbie:

var minimumTwirl = 0;
var maximumTwirl = 12;

private var originalAngle: float = 0;
private var tTwirlIn: float = 1.0;
private var tTwirlOut: float = 1.0;
private var duration: float;
private var trip = false;

function Start(){
var Twirl: TwirlEffect = Camera.main.GetComponent(TwirlEffect);
Twirl.angle = 0;

}

function Update (){
var Twirl: TwirlEffect = Camera.main.GetComponent(TwirlEffect);
var current = Twirl.angle;

if (tTwirlIn < 1.0 && trip == true) { // attempting to smooth if value other than zero
tTwirlIn += Time.deltaTime/duration;
Twirl.angle = Mathf.Lerp(current, minimumTwirl, tTwirlIn);
}

if (trip == true && tTwirlIn > 1){ //start oscillation of value

Twirl.angle = Mathf.Sin(Time.time)*15;
}

if (tTwirlOut < 1.0 && trip == false) { //this works surprisingly well!??

tTwirlOut += Time.deltaTime/duration;
Twirl.angle = Mathf.Lerp(current, minimumTwirl, tTwirlOut);
}
``````

A coroutine would be an idea.

Something like this:

OnTriggerEnter starts the coroutine.
OnTriggerExit sets a flag to indicate it should lerp back to zero and then quit the coroutine.

Your cotroutine would look something like this:

``````while( exit Flag Is Not Set )
{
do the oscillation for one frame
yield return 0; // Wait for the next frame
}

// Lerp back to zero
while( its not zero )
{
MoveTowards zero.
yield return 0; // Wait for the next frame
}
``````

Karl