On Button press Move Value toward

Hellow,

In short, I want to move a value toward another one WHILE the associated button is pressed.

At the moment, i've tried Mathf.Lerp and Mathf.MoveTowards but no success.

This is the assiociated part of the code.

var power = 0;
var speed = 5;
var powermax = 5000;
var powermin = 100;

    if(Input.GetButton("Pin")) {
         power = Mathf.MoveTowards(powermin, powermax, speed * Time.deltaTime);

    }

At the moment, all it does is after 1-2 second, the power switch from 0 to 110 and stay there.

PS: To be clear, there is no physics involved in this snippet of code. All I'm trying to do is to move a value A toward a value B within an incrementation speed, this only while the associated key is pressed.

Example:

Charging a Spell Power: Initial power = 0 Final power = 1000 Increase power by 10 per second until it reach Final power.

Hey Oninji,

If I understand your question... It seams to be a reoccurring topic on the forums... See, you can't add force, speed, etc. If you aren't already moving...

Example:

If your standing still, and think "I want to run", you can't run unless you start moving...

If I were you I would give your player movement first, then give it a "speed boost"...

-Gibson

This will change the power value linearly from min to max. It uses Mathf.Lerp, and the amount that the lerpAmt changes by is affected by the timeToMove.

var timeToMove : float = 5.0;       // time to move (in seconds)
private var lerpAmt : float = 0.0;  // current lerp 't' amount (private, 0-1)

if ( Input.GetButton("Pin")         // if pin is pressed
   && lerpAmt < 1 )                 // and lerpAmt is not already at max
{
   lerpAmt += Time.deltaTime / timeToMove;
   power = Mathf.Lerp(powermin, powermax, lerpAmt);
}

The code is untested but should work. Let me know if you have any troubles.