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 Omti1990 · Dec 06, 2018 at 08:26 PM · uicoroutinerecttransformpanel

Smoothly moving a rectTransform by script

So I'm building an interface right now and I have a number panel that I want to move around. And it sort of works. I've been using a coroutine to do this, but I'm facing the problem that it either just snaps to the target, or just moves minimally (depending on where I put the yield return null;.

In the following case it snaps:

 IEnumerator MovePanelToPosition(int activeNumber)
     {
         while (Mathf.Abs(rectTransform.anchorMax.x - (startAnchorMax.x - (xChange * (activeNumber)).x)) > 0.02f)
         {
             //Debug.Log(Mathf.Abs(rectTransform.anchorMax.x - (xChange * savedNumber).x));
 
             rectTransform.anchorMax -= xChange / 100 * movementfactor;
             rectTransform.anchorMin -= xChange / 100 * movementfactor;
 
 
         }
         yield return null;
     }



In the other case it just ends the coroutine after doing the while once (I think).

 IEnumerator MovePanelToPosition(int activeNumber)
     {
         while (Mathf.Abs(rectTransform.anchorMax.x - (startAnchorMax.x - (xChange * (activeNumber)).x)) > 0.02f)
         {
             //Debug.Log(Mathf.Abs(rectTransform.anchorMax.x - (xChange * savedNumber).x));
 
             rectTransform.anchorMax -= xChange / 100 * movementfactor;
             rectTransform.anchorMin -= xChange / 100 * movementfactor;
 
             yield return null;
         }
         yield return null;
     }

I really just want it to move smoothly to the target location without needing to use animations. This works easy enough for normal transforms, so I'm not sure what's the issue with the rectTransform / what I'm doing wrong.

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
1
Best Answer

Answer by Vega4Life · Dec 06, 2018 at 09:45 PM

Try something like this:

  IEnumerator MovePanelToPosition(int activeNumber)
      {
          while (Mathf.Abs(rectTransform.anchorMax.x - (startAnchorMax.x - (xChange * (activeNumber)).x)) > 0.02f)
          {
              //Debug.Log(Mathf.Abs(rectTransform.anchorMax.x - (xChange * savedNumber).x));
  
              rectTransform.anchorMax -= xChange / 100 * movementfactor;
              rectTransform.anchorMin -= xChange / 100 * movementfactor;

              // TRY THIS LINE, wait for a frame //
              // Waiting a frame should give it enough time to not look instant
              // If the movement isn't fast enough, possible add a speed variable and multiply it to the movementFactor
              yield return new WaitForEndOfFrame();
          }
      }
Comment
Add comment · Show 4 · 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 Omti1990 · Dec 06, 2018 at 09:49 PM 0
Share

Thank you very much. This works.

Any idea why the original ones didn't? They worked well enough for moving gameObjects around.

avatar image Vega4Life Omti1990 · Dec 06, 2018 at 10:06 PM 1
Share

The first one is just a loop without any delay in there, thus it just snaps. It exits the loop hits the yield and exits the coroutine.


The second one kind of works because yield return null... in a way acts like WaitForEndOfFrame(). Yield return null resumes on the next frame after the Update() call, but WaitForEndOfFrame will resume on the current frame after all the things render. Also, in your case, you already had a yield in the coroutine, so you didn't need the last on. You only need multiple yields if you plan on another delay later in the coroutine.


To me, if movement is something I am messing with, I think it's best to stick with WaitForEndOfFrame(). It literally resumes at the end of frame, after rendering, etc. You get some smooth movement that way.

avatar image TreyH · Dec 06, 2018 at 10:10 PM 1
Share

Be sure to cache your yield instruction

 IEnumerator $$anonymous$$ovePanelToPosition(int activeNumber)
 {
     var yieldInstruction = new WaitForEndOfFrame();
     //...
avatar image Vega4Life TreyH · Dec 06, 2018 at 10:44 PM 1
Share

Indeed. Though, you would cache it outside of the coroutine, so all other coroutines could use it. You wouldn't see much of an improvement, unless there were quite a few coroutines. I wouldn't cache it for just one.


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

167 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

Related Questions

How to size RectTransform to World Coordinates: RectTransform.SetWorldCorners? 0 Answers

Fill empty space in parent in UI Layout 1 Answer

How to move Unity UI Panel out of screen? 1 Answer

How can I make a UI menu appear with its top left corner in the position of the mouse on screen? 1 Answer

Button onClick events will not be triggered 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