Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lhixan · Jan 14, 2017 at 05:14 PM · c#waitforsecondscoroutinesienumeratorvector3.lerp

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Jan 16, 2017 at 05:27 AM 0
Share

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.

avatar image
0

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));
}
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges