Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jun 01, 2021 at 10:27 AM by anonimogk for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by anonimogk · May 29, 2021 at 03:32 PM · coroutinetransform.positioncoroutinesienumeratorupdate function

coroutine in update method

Hi, I am new at unity and I need your precious help for controlling coroutine method. The thing is I want my object to move forward and then back. It moves forward with no problem, but when it starts to move backward, it also try to move forward. so it flickeringly comes back to originial point. I dont know why it works right for forward but not work properly when it moves backward. I try to control it with some boolean values but couldnt figure out to appropriate usage. If you could hep, I really really be appreciated. Here is my code part;

 _pushing = false;
 _returning=false;
 private void Update
         {
             if (Input.GetMouseButtonDown(0))
             {
                 firstTouchPos = Input.mousePosition;
             }
 
             if (Input.GetMouseButton(0))
             {
                // does something
             }
 
             //if mouse released
             else
             {
                 rb.velocity = Vector3.zero;
                 StartCoroutine("Push", 2);
 
             }
         }
         
       private IEnumerator Push(float duration)
       {
            
             if (!_pushing) 
         {
             TransformMovingPusher.position += Vector3.forward * 5 * Time.deltaTime;
             Debug.Log("forward!");
             yield return (new WaitForSeconds(3));
             _returning = true;
             _pushing = true;
         }
                     

         if (_returning)
         {
             
             Debug.Log("backward!");
             TransformMovingPusher.position += Vector3.back *5 * Time.deltaTime;
             yield return (new WaitForSeconds(3));
             _returning = false;
         }
 
      
    }

I want it like this alt text

and this is the output part. I have 2 different backward ouputs?? alt text

1.jpg (62.1 kB)
2.png (27.9 kB)
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by nicoleskryp · May 29, 2021 at 04:36 PM

Use Input.GetMouseButtonUp(0) to accurately get when the mouse is released. The way you're using (Input.GetMouseButton(0)) recieves too many repetitive calls. Read about them all here: https://docs.unity3d.com/ScriptReference/Input.html

I'm not sure why you have so many mouse clicks but if you want the user to click and it moves forward for duration, then moves backwards for same duration just do this:

 void Update()
     {
         //Only gets called once on mouse release
         if (Input.GetMouseButtonUp(0))
         {
             //Call coroutine
             StartCoroutine(Push(2));
         }
     }
 
     private IEnumerator Push(float duration)
     {
         float timeElapsed = 0;
 
         while (timeElapsed < duration)
         {
             print("moving forward");
             TransformMovingPusher.position += Vector3.forward * 5 * Time.deltaTime;
             timeElapsed += Time.deltaTime;
             yield return null;
         }
         
         //Reset time to move backwards for same duration
         timeElapsed = 0;
 
         while (timeElapsed < duration)
         {
             print("moving back");
             TransformMovingPusher.position += Vector3.back *5 * Time.deltaTime;
             timeElapsed += Time.deltaTime;
             yield return null;
         }
     }






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

Follow this Question

Answers Answers and Comments

132 People are following this question.

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

Related Questions

Make a function that can return string or IEnumerator 2 Answers

Coroutine Not Working 2 Answers

Coroutines IEnumerator not working as expected 2 Answers

Use Coroutine when object rotates 2 Answers

How do Coroutines behave with Triggers? 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