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 ronronmx · May 31, 2011 at 01:15 AM · positionvector3floatprecisionrectangle

Precision problem when moving Rect values

So I'm having a hard time making sure my Rect.x position always ends up at 0. It's always higher or lower, and if I use Mathf.RoundToInt, I get random values like 1, -2, 0, -3, 2, -1....

Here's my code below:

 public IEnumerator SlideScreen( RectValues mainRect, int speed, int xPos, bool slideLeft ) {
         
         // Mathf.RoundToInt force the posX to end-up with a rounded value (eg; 0 instead of 0.2562)
         // This is important when sliding the screen back to 0, to make sure it always ends-up at 0.
         
         if( !slideLeft )
         {
             // Move Rect right until it reaches its destination
             while( mainRect.posX < xPos )
             {
                 mainRect.posX += Mathf.RoundToInt( ( speed * Time.deltaTime * 10 ) );
                 yield return null;
             }
         }
         else
         {
             // Move Rect left until it reaches destination
             while( mainRect.posX > -xPos )
             {
                 mainRect.posX -= Mathf.RoundToInt( ( speed * Time.deltaTime * 10 ) );
                 yield return null;
             }
         }
     }

What I do with this code is move my Rect.x position to 150, then back to 0. As I said, the problem is that I can never guaranty Rect.x position to be 0 everytime, it jumps between +1 2 3 and -1 2 3

How can I force it to be 0 everytime? Thanks for your time!

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

Answer by aldonaletto · May 31, 2011 at 03:06 AM

Just force Rect.x to the desired position right after the final loop, like this:

     // Move Rect left until it reaches destination
     while( mainRect.posX > -xPos )
     {
         mainRect.posX -= Mathf.RoundToInt( ( speed * Time.deltaTime * 10 ) );
         yield return null;
     }
     mainRect.posX = -xPos; // <--add this line
 }

}

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 ronronmx · May 31, 2011 at 10:00 PM 0
Share

Thanks guys, exactly what I was looking for! Both your help is greatly appreciated.

avatar image
1

Answer by Molix · May 31, 2011 at 03:09 AM

I'd probably just do:

 while( mainRect.posX > -xPos )
 {
   yield return null;
   mainRect.posX -= ( speed * Time.deltaTime * 10 );
 }
 mainRect.posX = -xPos;

That way you'll check it immediately after changing it, and if it is bad you'll clamp it before yielding and using the bad value elsewhere.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

can I change float to vector3 on euler ?? 1 Answer

Losing float precision when adding to Vector3 1 Answer

Vector3 using Doubles Instead of Floats 2 Answers

How do I preserve float accuracy in transform.position? 1 Answer

transform.position = new Vector 3 NOT moving to correct position? 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