• 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 IPS · Feb 23, 2013 at 05:52 PM · c#rotationprofiler

Interesting Rotation Detection Problem

So I've been racking my brains for the past couple of days trying to figure out a rotation problem I've been getting.

Basically I've set my cube up to rotate right 90 degrees by detecting when its at 90 degrees x rotation which works fine. But when I rotate it back to 0, it fails to detect when the angle is at 0 and thus fails to stop.

 void LateUpdate()
     {
         
         if(rightKeyPressed == true)
         {
             
             //Rotate around the v3Pos, which is a pivot point
             transform.RotateAround(v3Pos,  transform.right, Time.deltaTime * cubeSpeed);
             
             //Stops at 90 degrees, detects fine
             if(xAngle == 90)
             {
                 transform.RotateAround(v3Pos, -transform.right, Time.deltaTime * cubeSpeed);
                 print ("xAngle = 90.0");
             }
             
         } else if(leftKeyPressed == true)
         {
             
             //Rotate back to 0 degrees, works fine
             transform.RotateAround(v3Pos, -transform.right, Time.deltaTime * cubeSpeed);
             
             //Fails to detect
             if(xAngle == 0.0)
             {
                 transform.RotateAround(v3Pos, transform.right, Time.deltaTime * cubeSpeed);    
                 print ("xAngle = 0.0");
             }
             
         }
         
     }

If anyone has any ideas why this is let me know!

Comment
Add comment · Show 2
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 robertbu · Feb 23, 2013 at 06:03 PM 0
Share

I'd like to see a bit more of your script. In particular I'd like to see how xAngle is being set. I suspect you are are setting the angle from transform.position which has several different issues.

avatar image IPS · Feb 23, 2013 at 06:18 PM 0
Share

Hi Robertu,

I'm creating it in the public class as:

private float xAngle;

and then setting it in the update function like this:

xAngle = transform.rotation.eulerAngles.x;

1 Reply

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

Answer by robertbu · Feb 23, 2013 at 07:17 PM

That's what I though. I'm surprised the "if (xAngle == 90)" part works. The first issue you have is there is no guarantee that the angle will hit any target given you are using deltaTime. That is the angle could go from 89.999 to 90.0001 never hitting 90. The second issue is the direct comparison of floating numbers. Even when a calculation should turn out even number, you may in fact have something like 0.00000000000000000000001, and the code "x == 0" will fail. And then you have the issue of the 0/360 boundary to deal with. Plus, if you do other rotations with your game object, there is no guarantee that a 45 degree rotation about the X-axis will be represented by a euler value of 45 (i.e. the same rotation can often be represented my multiple different euler settings).

Here is an attempt at a "fix" for your code. It still suffers from the issue that if you have other rotations going on, a 90 X axis rotation might not be euler 90, and therefore this code can fail.

 public class RotateUpDown : MonoBehaviour {
     
     public float cubeSpeed = 22.0f;
     
 void Update() {
         float xAngle = transform.eulerAngles.x;
         if (xAngle > 180.0f)   // Normalize the angle to -180 to 180
             xAngle -= 360.0f;
  
        if(Input.GetKey (KeyCode.UpArrow)) {
           if (xAngle < 90.0f)
              transform.RotateAround(transform.position,  transform.right, Time.deltaTime * cubeSpeed);
 
  
        } else if(Input.GetKey (KeyCode.DownArrow)) {
         if (xAngle > 0.0f)
            transform.RotateAround(transform.position, -transform.right, Time.deltaTime * cubeSpeed);
        }
  
     }
  }
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 IPS · Feb 23, 2013 at 08:51 PM 0
Share

This is great! Athlough the cuboid goes over a little because the detection is a little slow. Thanks Rob

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How do i rotate an object "Once" 1 Answer

Player clipping through objects as they rotate 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