- Home /
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.
From your question, I wasn't sure if you wanted it to reset when you let go of "Pin" - if that's the case, comment on my answer and I'll update the code.
Answer by · Oct 12, 2010 at 11:00 PM
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.
Answer by AVividLight · Oct 12, 2010 at 10:21 PM
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
No, I simply want to move a value.
The rest of the script already work with a set Value. All I need to do is to make the "Power Value" move towards his "$$anonymous$$ax power". A bit like, charging an attack.
There is no real movement happening, it's just a question of moving a numerical value.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Maths with variables 2 Answers
Calculations Wrong? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers