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
1
Question by Teku-Studios · Feb 10, 2015 at 08:23 PM · physicsvaluesissuesrotations

Physics madness: 270º != 270º - Please help

I've been developing a game in Unity for almost 3 years and I'm still trying to understand how to deal with rotations. One very specific situation is breaking my code and I need help on how to fix that. Here's my case scenario:

I have an object which can be rotated to absolute values (0 - 90 - 180 - 270), and I need to check that initial rotation before applying some other transformations. I do this:

 int angleZ = (int) m_spriteTransform.rotation.eulerAngles.z;
 
 if(angleZ == 270)
      Do some stuff;
 else if(angleZ > 180 && angleZ < 270)
      Do other stuff;

I need to do that check for reasons that are not important now, but here's what happens. I'm forcing its rotation to be 270 degrees

 int angleX = 0;
 int angleY = 0;
 int angleZ = 270;
 
 m_spriteTransform.rotation = Quaternion.Euler(angleX, angleY, angleZ);

And there is NOTHING at all changing that object's rotation. Anything. However, when I do the first check, the code if(angleZ == 270) returns FALSE, and the code else if(angleZ > 180 && angleZ < 270) returns TRUE, which makes no goddam sense.

I'm setting breakpoints and also a Debug.Log info log and, after checking both, angleZ returns 270, so I don't know what is happenning.

How can I deal with this properly?

Comment
Add comment · Show 4
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 tanoshimi · Feb 10, 2015 at 08:26 PM 0
Share

$$anonymous$$y guess is that you have an error in your logic for converting/comparing between ints and floats, but it's hard to tell from isolated code snippets.

Can you post complete simplified code sample that reproduces the problem?

avatar image giulio-pierucci · Feb 10, 2015 at 08:52 PM 1
Share

But... angleZ taken from rotation eulerangles maybe different i think (270 and -90) for example.

if you don't need SIGNED angle, you can use Vector3.Angle (between tranform.up and vector3.up?)

http://docs.unity3d.com/ScriptReference/Vector3.Angle.html

otherwise, something like this:

 var angle:float = Vector3.Angle(vectorA, vectorB);
     var cross:Vector3 = Vector3.Cross(vectorA, vectorB);
     if (cross.y < 0) angle = -angle;



avatar image tanoshimi · Feb 10, 2015 at 09:05 PM 1
Share

Yes - there's lots of potential issues - Euler angles provide a convenient description of any rotation in three-dimensional space using three numbers, but they're not unique. See Gimbal Lock. That's why Unity uses quaternions.

avatar image meat5000 ♦ · Feb 10, 2015 at 09:29 PM 0
Share

Precondition it to make sure that angle is never out of 0-360 range. Note this only works if the angle is out by <360.

 if(angleZ < 0)
 {
     angleZ = angleZ + 360;
 }
 else if(angleZ > 360)
      {
          angleZ = angleZ - 360;
      }
 
 if(angleZ == 270)
 Do some stuff;
 else if(angleZ > 180 && angleZ < 270)
 Do other stuff;

Edit : Sorry, I just realised this probably isnt relevant.

1 Reply

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

Answer by Sir_Everard · Feb 10, 2015 at 08:56 PM

The eulerAngles variable is not what you need to modify as it is just a return value.

http://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html

There are a number of rotation functions you can perform to rotate an object.

I think the one you would benefit from most is:

transform.Rotate((float)angleX , (float)angleY, (float)angleZ);

http://docs.unity3d.com/ScriptReference/Transform.Rotate.html

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 Teku-Studios · Feb 11, 2015 at 09:35 AM 1
Share

Yep, I reworked the code and switched to that. $$anonymous$$uch more precise behaviour and the erratic compare values are gone. Thanks!

By the way, I was doing rotation = Quaternion.Euler(angleX, angleY, angleZ), but I got that wrong in the OP.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I have been struggling with a GUI window with table data for over a week now. What am I doing wrong? 1 Answer

How will i update my table values every time? 1 Answer

Passing values every time to table? 0 Answers

Physics changes in 3.3 0 Answers

move player to its rotating direction? 2 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