#Lerp from 0-1 to 1-0
1 messages · Page 1 of 1 (latest)
Of before the coroutine or after that? Like the code inside update()?
Whatever you currently have at the moment.
If I recall from last night - You wanted to go from 0 to 1 from a random time interval, and then go from 1 down to 0. A coroutine will help solve you this problem because you can then run two logic inside one method.
You'd yield instruction going up, then once variable t reaches to 1, you can either wait, or continue to invoke the next iteration, which subtracts t down to 0.
Again - a Coroutine helps derive your logic further, after "elapsedTime" has reached to 1.
There's nothing in there that specify "Hey! I've reached to 1, now subtract this value!"
Someone has suggested to use Mathf.PingPong();
i know ill do that now, you just asked for my code
Mathf.PingPong(0,1) will always return 0f.
i see
Consider caching your result of Image into a variable reference to avoid repeatingly calling .GetComponent() method. It's very expensive to call every frame.
Missing yield instruction.
what would it be for? because if what your saying is yield return new _
yield return null would instruct unity to skip the current frame and resume the next code block on the next frame.
Essentially, like Update() method. But you resume the remaining code on the next frame.
For example -
IEnumerator Test()
{
Debug.Log("Hello world! I'm at " + Time.time + "!");
yield return new WaitForSeconds(5f); // wait for 5 second before resuming the rest of the code!
Debug.Log("Hi again, I'm back five seconds later! " + Time.time);
}
using yield return null essentially skips the current frame, and does not create allocation when calling this, as opposed to calling new WaitForSeconds().
You would place yield return null where you want to check the code on the next frame. That's entirely up to your design decision.
For example, you could put that inside a while loop call, and update your elapsedTime value, until your while condition has met it's condition where elapsedTime is equal to 1.
im trying to wrap my head around this
Read it out loud?
Your from variable is not getting update and will always remains zero.
well what i have above is just 0-1 but that doesnt even work
Oh yeah sorry my bad. Did you start the coroutine?
Usually a StartCoroutine(LerpMethod(_image)); would do the trick. Please do not invoke this inside the update method unless you have some if conditions to fire it once
oh crap
ok
so its in start now but it only lerps from 0-0.25
it only works if i do while (elapsedTime <= 4) not (elapsedTime <= 1)
oh
thats what the yield return null does
Your elapsedTime condition is not reading the same value as your percent.
hence, your elapsedTime will reach to 1 before your percent could.
Should probably check percent instead of elapsedTime.
aight
https://pastebin.com/g2CG18e2 this worked perfectly but ill change it to that
You can add additional parameter inside your LerpMethod to introduce time, e.g. LerpMethod( Image image, float time ) then you can replace all the magic number (4) to time.
One thing to keep in mind is that calling StartCoroutine() will not stop any previous running Coroutine. Create a variable to hold the handler to existing coroutine, and when you need to start a new instance, you'd either StopCoroutine(handler) and then start a new one, or expose variable to updating current running coroutine implementations so that you have a much more interactive animation in your game.