A more elegant way to make a variable constantly return to 0?

I have a variable that may have a whatever numerical value placed into it, and then this numerical slowly pulls back to 0 at a steady rate. I am currently using this in code:

[script] //slow momentum
if(moveMomentum.x > 0)
{
moveMomentum.x -= Time.deltaTime * 2000;

}
else if (moveMomentum.x < 0)
{
moveMomentum.x += Time.deltaTime * 2000;

} [/script]

basically if movement is greater than zero, then subtract, if it is larger, then add.

Then I thought, this seems like a really common thing and unity probably has some built in method, or a more elegant way to do this. So I ask, is this the best way to do this? Or does unity have any built in functionality to do this in a more automated way?

I am not sure I understand your usage case correctly.
But to clamp the Value you can use Mathf.Clamp() That would prohibit the value to get below zero.

The way you are doing it right now, you will always have movement. Except if you get really really lucky and hit the number 0 :smiley:

You can try using Mathf.Lerp:

http://unity3d.com/support/documentation/ScriptReference/Mathf.Lerp.html

@eem: what does this variable represent in the game?

In C#:

float aFloatVar;

void Awake()
{
StartCoroutine(FadeToZero(aFloatVar));
}


public IEnumerator FadeToZero(float myVar)
{
while(true)
{
Mathf.Lerp(myVar, 0.0f, 0.5f); //Will move halfway to zero on every frame.  The farther you are the faster it moves towards 0
/*
myNewVar -= Mathf.Sign(myVar) * 0.5f;
if(Mathf.Sign(myNewVar) != Mathf.Sign(myVar)//If you crossed 0
{
MyVar = 0;
}
else
{
myVar = myNewVar;
}
*/ This second method will move at a constant speed(0.5 in this case) 
}
}

Thank you Ntero, this answers my question.

However this brings up a number of other questions, would starting a coroutine that runs indefinitely actually be more efficient than just doing the same thing in the Update loop? I have maybe 10 variables that need to be recentered to 0, meaning, I will need 10 courotines going constantly. Does Unity internally manage repetitious code execution more efficiently when processes are split out between coroutined functions? Or would it process them more efficiently all stacked in the update function?

As I am understanding this based on your code, if I start a bunch of coroutines, will the coroutined function execute all of it’s code only once per the update function updating it’s code? If that is true, then what would be the usage of yield command? Based on the documentation it seemed to me that at the end of such a coroutined function you would need to put yield, so then it waits until the next update of the update function to run it’s code again. Is that not how it works?

Also I’m wondering, will the coroutine function execute it’s code before or after the update function? And will it do it before or after the lateUpdate function?

I also do not fully understand why it is necessary to use IEnumerator for coroutined functions. Why is that necessary? In javascript you could coroutine any function.

Not true; in JS a function is automatically typed as IEnumerator if it’s a coroutine, and can’t return anything else.

function MyCoroutine () {
    yield;
}

is the same as

function MyCoroutine () : IEnumerator {
    yield;
}

If you try

function MyCoroutine () : int {
    yield;
}

then you get an error.

–Eric