- Home /
Run Coroutine once, but finish Lerping
I'm trying to make an image come up when I press a UI.Button, and go back down after 2 seconds, but at the moment as soon as I press the button, it will run everything in the coroutine about 180 times (obviously because it's in the Update()), but when I put an isLerping bool check on the if (wrongAnswer), it will run everything once, like it should, but the Lerp won't work. What it'll do is, the image pops up about 1 frame so you only see the very top of the image, then after 2 seconds, pops right back down, not in a smooth transition way, but in a snapping way. I feel like I'm making a very obvious and dumb mistake here but I honestly can't wrap my mind around it, help would be greatly appreciated.
Edit: The UI.Button calls FoutAntwoord() when clicked, I forgot to add that to the above.
using UnityEngine; using System.Collections;
public class Questions : MonoBehaviour {
private bool wrongAnswer = false;
public GameObject imageWrong;
public GameObject resultPos1;
public GameObject returnPos1;
void Start ()
{
wrong.gameObject.SetActive(false);
}
void Update ()
{
if (wrongAnswer)
{
StartCoroutine(LerpFalse());
}
if(!wrongAnswer)
{
StopCoroutine(LerpFalse());
}
}
public void FoutAntwoord()
{
wrongAnswer = true;
}
void Wrong()
{
imageWrong.transform.position = Vector3.Lerp(imageWrong.transform.position, resultPos1.transform.position, 0.2f);
}
void WrongBack()
{
imageWrong.transform.position = Vector3.Lerp(imageWrong.transform.position, returnPos1.transform.position, 0.2f);
}
IEnumerator LerpFalse()
{
wrong.gameObject.SetActive(true);
Wrong();
yield return new WaitForSeconds(2);
WrongBack();
yield return new WaitForSeconds(1);
wrong.gameObject.SetActive(false);
wrongAnswer = false;
}
}
Answer by HenryStrattonFW · Jan 15, 2017 at 04:06 PM
I think the problem here is that you are starting the coroutine every update whilst wrongAnswer is true, but wrong answer is only set to false at the END of the coroutine, some 3 seconds later. Meaning you are starting many coroutines each one calling Wrong() at least once before waiting.
I suggest a couple of solutions here. The first, is to have a way of knowing if you coroutine is running, and only calling StartCoroutine if it isn't already running. You can either do this via a boolean flag that you can set at the start of the coroutine, and reset at the end, or could actually store the Coroutine object returned by StartCoroutine and use that instead of the booelan flag (but still having to null that value at the end of the coroutine).
Finally you could also (would suggest doing this in addition to the above) Rework your code so that the detection of a wrong answer can trigger a single explicit function call instead of just setting a flag, since then you only have one place to trigger the coroutine and don't have to worry about it potentially happening in each update loop at all. since the wrong answer event should only occur periodically instead of every frame, so the code could be structured to reflect that as well.
Hope this helps.
That's the first part of the problem. The second problem is that the LerpFalse()
coroutine function only calls the Lerping functions Wrong
and WrongBack
once. Calling Lerp once won't move anything smoothly, but just change the position once (20% from starting position towards target position since the last parameter is 0.2f).
Ins$$anonymous$$d of WaitForSeconds(2)
you should make a loop that uses that 2 seconds to Lerp the object position.
...
Wrong();
var time = 0f;
while (time < 2f) {
time += Time.deltaTime;
WrongBack();
yield return null;
}
Also, to be exact, the way Lerp is used here, the object will never reach exactly the target position. You always move the object "20% closer to resultPos1.transform.position
" . Lerp will only return the target position if you call it with the last parameter being 1f.
Answer by steo · Jan 16, 2017 at 07:53 AM
You can use DoTween (free in asset store) to do program animations (move, fade, etc). It's awesome and simple to use. They add extension methods for many Unity components (you need using DG.Tweening;
to use it) and your code will be like:
public void FoutAntwoord() {
wrong.gameObject.SetActive(true);
imageWrong.transform.DoMove(resultPos1.transform.position, 0.2f)
.OnComplete(() => {
imageWrong.transform.DoMove(returnPosition.transform.position, 1f)
.SetDelay(1.8f)
.OnComplete(() => wrong.gameObject.SetActive(false));
});
}
or
public void FoutAntwoord() {
wrong.gameObject.SetActive(true);
DoTween.Sequence()
.Append(imageWrong.transform.DoMove(resultPos1.transform.position, 0.2f))
.AppendInterval(1.8f)
.Append(imageWrong.transform.DoMove(returnPosition.transform.position, 1f))
.AppendCallback(() => wrong.gameObject.SetActive(false));
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Yield/OnTriggerEnter Help C# 1 Answer
Proper Way to Wait in C# 1 Answer
Mysteries of yield 1 Answer