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 /
  • Help Room /
avatar image
0
Question by SebastianMatuz · Oct 10, 2020 at 12:57 AM · movementif-statementscoroutinesc++

Contrary if statements being called at the same time

Hi, i'm currently having a problem with this specific piece of code

 protected IEnumerator JumpTowards(Vector3 p)
     {
         animator.SetBool("jumping", true);
         Vector3 currentPos = transform.position;
         Vector3 targetPos = new Vector3(transform.position.x, transform.position.y + jh, transform.position.z);
         while(nearbyPlayer)
         {
             if(transform.position.y >= targetPos.y)
             {
                 transform.position = Vector3.MoveTowards(transform.position, currentPos, movSpeed * Time.deltaTime); Debug.Log("1");
             }
             else if (transform.position.y < targetPos.y)
             {
                 transform.position = Vector3.MoveTowards(transform.position, targetPos, movSpeed * Time.deltaTime); Debug.Log("12");
             }
             yield return null;
         }
         animator.SetBool("jumping", false);
         jumping = false;
         yield return null;
     }

This coroutine is supposed to move up (y) and object until it reaches it's target destination and then go down to it's original position. The problem i have is that it works perfectly when going up ( else if (transform.position.y < targetPos.y) ) but when it gets there it's supposed to stop being called and then go down but apparently the other if statement ( if(transform.position.y >= targetPos.y) ) gets called simultaneously with the second if statement ( I know that because of the Debug.Log()).

Any help is greatly appreciated.

PD: I also checked that the whole method gets called only once.

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

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Oct 10, 2020 at 01:08 AM

No, it's not called simultaneously, you just jump between the two every frame. Your first if statement only moves down towards the original position if your the current position of your object is greater or equal to your target position. Once you moved one tiny step downwards your object is no longer above your target position and therefore your second condition will take action and you move back up. The next frame the first condition will be true again and you again move down a tiny bit. This repeats endlessly until nearbyplayer is false.

If you want it constantly move up and down while that boolean is true, do something like this:

     while(nearbyPlayer)
     {
         while(transform.position.y >= targetPos.y)
         {
             transform.position = Vector3.MoveTowards(transform.position, currentPos, movSpeed * Time.deltaTime); Debug.Log("1");
             yield return null;
         }
         while (transform.position.y < targetPos.y)
         {
             transform.position = Vector3.MoveTowards(transform.position, targetPos, movSpeed * Time.deltaTime); Debug.Log("12");
             yield return null;
         }
         yield return null;
     }

If you want to be able to interrupt the movement as soon as the boolean turns false, you can just include it in the condition of the two inner while loops.

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

204 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 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

Play animation when WASD is pressed, stop animation when none of them are pressed (Help!!) 0 Answers

How to cancel a if statement once touching a collider 1 Answer

how can i repeat an if statment for x sec? 1 Answer

Smooth Forward Movement with a CoRoutine 1 Answer

Rotating object with Lerp in coroutine with correct speed 0 Answers


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