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 /
avatar image
0
Question by galvakojis · Dec 04, 2014 at 06:48 PM · normalize

How to normalize rotation?

So on double mouse click my parent object rotates clockwise by 90 degrees, so it should be 0, 90, 180, 270 (0 or 360). But reality are different sometimes a have some floating number egz(90.000001), why so, gow to normalize it?

 private int clickCounter = 0;
  float targetX = 90.0f;
  float timer = 0.5f;
 
      void OnMouseDown(){
         screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
         offset = gameObject.transform.parent.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
         gameObject.renderer.sortingOrder = 2;
 
         if (Input.GetMouseButton (0)) {
 
             mouseClickStart = true;
             Invoke("checkMouseDoubleClick",timer);
             clickCounter++;
         
             if (clickCounter == 2) {
                 //gameObject.p.transform.rotation = Quaternion.Euler (0,0,targetX);
                 gameObject.transform.parent.rotation  = Quaternion.Euler(0,0, targetX);
         
                 targetX = targetX-90.0f;
 
 
             }
         }
     }
 
     private void checkMouseDoubleClick()
     {
 
         if(clickCounter > 1){
             Debug.Log("Double Clickedd");
         }else{
             Debug.Log("Single Clicked");
         }
         mouseClickStart = false;
         clickCounter = 0;
     }






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 RudyTheDev · Dec 05, 2014 at 01:16 AM

That's a limitation of floating point precision variables. Numbers in decimal system don't necessarily match with their binary representation. In Unity, it is very prevalent (or rather, noticeable because it shows them all over the place). There isn't anything you can do directly about it. You can manually round them off if you need whole numbers. But so long as they are part of GameObject/calculations/physics/change, they can eventually get jiggled.

Edit: Generally, floats are compared with Mathf.Abs(a - b) < epsilon, where epsilon is your chosen precision/tolerance (this is quick & dirty approach and there are downsides to it if you google around about this topic). You can use Unity's Mathf.Approximately() as tanoshimi points out below.

Comment
Add comment · Show 3 · 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 galvakojis · Dec 09, 2014 at 11:30 AM 0
Share

but i need to combine two and more objects into one, when they have equal rotation, how to do that?

avatar image tanoshimi · Dec 09, 2014 at 11:34 AM 2
Share

You don't ever compare exact equality of two floating point numbers - you compare whether they are the same within a certain tolerance using $$anonymous$$athf.Approximately

avatar image galvakojis · Dec 17, 2014 at 11:38 AM 0
Share

Thank you, but $$anonymous$$athf.Approximately, work only in first time of obj rotation if rotate obj in 720 degrees it is not "0", but a -2.003582e-05, and $$anonymous$$athf.Approximately doesn't work.

UPDATE, fixed problem with: if ($$anonymous$$athf.Abs(figure[0].parent.rotation.z - figure[2].parent.rotation.z) <= threshold)

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

magnitude normalize help 1 Answer

Normalizing a vector 1 Answer

direct and reverse proportion between variables 2 Answers

Getting the radius of a square 2 Answers

Vector2 Normalize ? 3 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