#Lerp from 0-1 to 1-0

1 messages · Page 1 of 1 (latest)

proven lotus
#

Test

#

@errant silo here’s the thread

errant silo
#

That's better - but now it's missing context.

#

Can you please share your code?

proven lotus
errant silo
#

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.

proven lotus
errant silo
#

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();

proven lotus
#

i know ill do that now, you just asked for my code

errant silo
#

Mathf.PingPong(0,1) will always return 0f.

proven lotus
#

i see

errant silo
#

Consider caching your result of Image into a variable reference to avoid repeatingly calling .GetComponent() method. It's very expensive to call every frame.

proven lotus
#

aight

#

so basically just doing this?

errant silo
#

Missing yield instruction.

proven lotus
errant silo
#

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);
}
proven lotus
#

I see

#

Where would I place the yield return null? at the end of the coroutine?

errant silo
#

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.

proven lotus
#

im trying to wrap my head around this

errant silo
#

Read it out loud?

#

Your from variable is not getting update and will always remains zero.

proven lotus
#

well what i have above is just 0-1 but that doesnt even work

errant silo
#

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

proven lotus
#

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

errant silo
#

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.

proven lotus
#

aight

errant silo
#

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.

proven lotus
#

I just did this and it works

#

thank you

errant silo
#

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.