• 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 Ihcia · May 11, 2015 at 09:18 AM · rotationfloatingfloatingpoint

rotation floating point error

Hello Folks,

I am trying to rotate a 2D-gameObject about 90° by clicking a button. So the rotation should stop at 90, 180, 270, 360. This works perfectly fine, but when it turns around it does not stop at the degrees 90, instead it stops by 90,000002F or when it turns to zero it totally turns crazy, and the number stops at -1.001791e-05. I allready know its the floating point error and i know why the computer writes the number not correct. but i want to make my code perfect because otherwise my controlls will not work.Heres my code... the degree variable will get -90F or +90F from another script when the specific button is clicked. Thanks a lot..

 using System.Collections;
 
 public class RotateScript : MonoBehaviour {
     
     float damping = 9F;
     public static float degree = 0;
     Vector3 rotation;
 
 
     
 
 
     void Start(){
 
     
     }
 
     void Update () {
 
         rotation = transform.eulerAngles;
 
 
         this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.Euler (0, 0, degree), Time.deltaTime* damping);
     
         if (Mathf.Approximately (rotation.z, 0F)) {
         
             rotation.z = 0;
         
         }
         if (Mathf.Approximately (rotation.z, 90F)) {
             
             rotation.z = 90;
             
         }
         if (Mathf.Approximately (rotation.z, 180F)) {
             
             rotation.z = 170;
             
         }
         if (Mathf.Approximately (rotation.z, 270F)) {
             
             rotation.z =270;
             
         }
         if (Mathf.Approximately (rotation.z, 360F)) {
             
             rotation.z = 0;
             
         }
     
     }
 
 }
Comment
Add comment · Show 1
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 Ihcia · May 11, 2015 at 07:09 AM 0
Share

Okay so ins$$anonymous$$d of of trieng to compare two floats with mathf.approximately, i should rewrite my controller something like that?

 if(buttonRight == true)
 {
 
 if($$anonymous$$athf.Approximately(RotateScript.degree, 0)||$$anonymous$$athf.Approximately(RotateScript.degree, 90)||$$anonymous$$athf.Approximately(RotateScript.degree, 180)||$$anonymous$$athf.Approximately(RotateScript.degree, 270$$anonymous$$athf.Approximately(RotateScript.degree, 360))
 {
 RotateScript.degree += 90;
 
 }


1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Umresh · May 11, 2015 at 10:02 AM

You have to manually check and clamp the rotation if you are using Quaternion.Slerp. And your degree variable keeps incrementing by +90 when degree = 360 you have to reset it to 0.

Comment
Add comment · Show 8 · 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 Ihcia · May 11, 2015 at 10:24 AM 0
Share

how to i check a rotation manually?

this is what i get from the unity documentation. So i dont know how to check these parameters other then i checked it before... can you help me out with a little description?

  transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);

And I am trying to check if the actual transform.eulerAngles.z is approximately 90, then it should turn the transform.eulerAngles.z to 90;

Or i tryied to check if the rotationscript.degree is approximately 0,90,180 etc. and then it should rotate 90 more...

avatar image Umresh · May 11, 2015 at 11:16 AM 0
Share

Try

 if(buttonRight == true)
  {
   if($$anonymous$$athf.Approximately(RotateScript.degree, 0)||$$anonymous$$athf.Approximately(RotateScript.degree,90)||$$anonymous$$athf.Approximately(RotateScript.degree,180)||$$anonymous$$athf.Approximately(RotateScript.degree, 270)||$$anonymous$$athf.Approximately(RotateScript.degree,360))
  {
 if(Rotate.degree == 360 )
 Rotate.degree = 0;
  RotateScript.degree += 90;
  
  }



avatar image Ihcia · May 11, 2015 at 12:32 PM 0
Share

I tryied that but now if the rotation is 270, i can click button, but nothing happens anymore, so there is no rotation...

avatar image Umresh · May 11, 2015 at 01:09 PM 0
Share

I tried your script and it worked it rotates by +90 on click Can you post edited script.

avatar image Umresh · May 11, 2015 at 01:27 PM 1
Share

Okay, if you are using -90 you have to negate the angles

 if ($$anonymous$$athf.Approximately (RotateScript.degree, 0) || $$anonymous$$athf.Approximately (RotateScript.degree, -90) || $$anonymous$$athf.Approximately (RotateScript.degree, -180) || $$anonymous$$athf.Approximately (RotateScript.degree, -270) || $$anonymous$$athf.Approximately (RotateScript.degree, -360)) {
             if(RotateScript.degree == -360)
                 RotateScript.degree = 0;
             RotateScript.degree -= 90;
             
         }
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I remove floating point errors when setting rotations with an editor script!? 2 Answers

Euler Angle problem in If check 1 Answer

Quaternion.ToAngleAxis is Unprecise? 1 Answer

Float the camera back to his position 1 Answer

EulerAngles value as floating point 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