- Home /
Answer by Moohasha · Sep 20, 2013 at 01:30 PM
Use the Lerp function in Mathf: http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Lerp.html
You might also look at doing this in a Coroutine instead of Update so that once you reach your target value you can just exit.
Hm, thanks!
If I understand it correctly, when I use
transform.position = Vector3($$anonymous$$athf.Lerp($$anonymous$$imum, maximum, Time.time*5), 0, 0);
it'll change over 5 seconds?
Quite the opposite. You are multiplying the time value by 5, meaning that time is increasing 5 times faster, not slower. You would need to divide by 5 for it to take 5 seconds.
Also, Time.time is the time since the game started. Since the t value in Lerp needs to be in the range [0, 1], this would only work for the first second of the game. I suggest using Time.deltaTime each frame and maintaining your own t value.
float t = 0;
void Update()
{
t += Time.deltaTime / duration;
value = $$anonymous$$athf.Lerp($$anonymous$$imum, maximum, t);
}
Hey sorry if this is kinda necroposting, i was wondering if you can get the 'time remaining' (like the time until it reaches the required result) from this correct answer to the question.
Answer by goo-muffin · Sep 20, 2013 at 01:02 PM
var value :float;
var periodLength :int; //Seconds
var amount :float; //Amount to add
function Update()
{
value = Mathf.Floor(Time.time * periodLength) * amount;
}
This is untested. Just try out. If this shouldn't work, then just look for Coroutine at the Unity Script Reference.
Hm, would make sense to make periodLength a float so I could use fractions of seconds as well.
And not necessarily adding. I need linear twine from one value to other over specific amount of time. That means also FRO$$anonymous$$ 30 TO 10 over 6.8sec.
I don't want to change thing BY some amount, just FRO$$anonymous$$ value TO value. Sorry if I came off offensive but just had argument with my in-laws, so...
Your answer
Follow this Question
Related Questions
Slowly making the player go faster. 0 Answers
referring to a particular material 0 Answers
Time of day script not working 2 Answers