- Home /
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);
}
Answer by karljj1 · Nov 20, 2013 at 09:11 AM
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
Thanks for your reply. I attempted to script what I'm trying to do. This is what I have so far but the Twirl.angle functions don't work perfect yet. I posted the code above.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122259im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can't click gameobject when over another trigger? 1 Answer
Increase Speed 1 Answer
Trigger and Collider Issues 1 Answer
OnTriggerStay still called after moving a parent gameobject 0 Answers