• 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 Oninji · Oct 18, 2010 at 08:10 PM · rotationjavascriptangle

Proper X Rotation Angle Detection.

Hellow, I need the be able to read the proper angle of my object.

But when I read it from Unity in my script,

(the reading part of the script)

AngleX = transform.eulerAngles.x;

It read it quite awkwardly,

It goes from 360(0) to 270, then from 270 to 360(0), then from 360(0) to 90, then from 90 to 360(0).

That's readings I get, on a SINGLE full rotation.

Edit:

Here a video http://www.megaupload.com/?d=WIEMPF70

Youtube for the ones who do not want to download a file. http://www.youtube.com/watch?v=gfTcPG7Nfd4

Just look the X Angle value in the inpsector, and you'll probably understand.

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 skovacs1 · Oct 18, 2010 at 08:40 PM 0
Share

This description makes little to no sense. What is a "proper angle"? What do those values mean? What are you doing when you get those values? It's next to impossible to answer a question that doesn't make sense and, while it is apparent that your English is very poor, could you please rephrase the question into something clearer?

avatar image Cyb3rManiak · Oct 18, 2010 at 09:16 PM 0
Share

Also, try using Transform.localEulerAngles.x and post back if it helps or not, and if it's closer to what you would expect to see.

avatar image Oninji · Oct 18, 2010 at 09:40 PM 0
Share

The big itself does little sense. It is as I say, What the X rotation reading return isn't the angle of the object On screen.

avatar image Oninji · Oct 18, 2010 at 09:41 PM 0
Share

Added a video, and local or not it doesn't work better.

2 Replies

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

Answer by Cyb3rManiak · Oct 18, 2010 at 11:50 PM

Ok. Now I understand what you were talking about.

You must understand that there is a big difference between Quaternions and Euler angles. You cannot use Euler angles to describe every possible rotation of the object if you simply rotate along the axes in an arbitrary way without some forethought, since there are cases in which a situation called "gimble lock" occurs. I won't go into explanations about how and why this happens, and ways to solve it. You can do a search in youtube or google. Plenty of material exists on this, but those flips you see (one of the axes you're not editing must be modified) are done to avoid that situation.

Anyway, just like Adam said - internally Unity stores the rotation as Quaternions. What you see exposed in the inspector is a representation of the local rotation in Euler angles, which can differ since there are several ways to describe each quaternion in euler angles, and to avoid gimble lock a representation which is less readable to you as a user is used, but will not produce gimple lock internaly. If you're going to rotate on more than one axis - stop using euler angles, or get frustrated very fast.

If you insist on doing it using euler angles - use the local rotation to your advantage. Setup your model this way (lets say your model has a part called "base", which has a child called "turret" - which moves):

  1. Rotate "turret" to the begining of the rotation where you want 0 degrees to be.

  2. Create a new game object and call it "turretParent". Make this object be a child of "turret".

  3. Enter the position (0,0,0) and rotation (0,0,0) for "turretParent" in the inspector. This will move the "turretParent" to the pivot of "turret" and align it to the exact rotation of "turret".

  4. Make "turretParent" be a child of "base".

  5. Make "turret" a child of "turretPivot".

Now if you look at the moving part in the inspector ("turret") - the rotation will be (0,0,0), even though you moved it before. It's just that relative to its parent ("turretPivot") - it's exactly in the same direction - and so if you rotate it around the X asis now - it will move from 0 to 360 without flipping anything. If you want to start from another number (lets say you want the starting position to be (90,0,0)) - between steps 4 and 5 - rotate "turretParent" 90 degrees on the X axis. Now when in stage 5 you parent "turret" to be a child of "turretParent" - they will have 90 degrees difference, and instead of saying (0,0,0) the inspector will read (90,0,0) even though the turrent is in the same position. It's just that it's rotation is now shown relative to the new game object, and not the base.

Comment
Add comment · Show 5 · 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 Jesse Anders · Oct 19, 2010 at 05:56 AM 0
Share

$$anonymous$$inor correction: you can in fact use Euler angles to describe every possible rotation of an object (gimbal lock doesn't have any bearing on what orientations can and cannot be represented).

avatar image Cyb3rManiak · Oct 19, 2010 at 12:59 PM 0
Share

Well yes, but you cannot go through them in sequence without making some corrections. As I stated - you can in fact represent any quaternion in several different ways using euler angles.

avatar image Jesse Anders · Oct 19, 2010 at 02:16 PM 0
Share

I see - I think it was just a wording/interpretation issue (i.e. I read your statement differently than you intended it).

avatar image Oninji · Oct 19, 2010 at 05:21 PM 0
Share

I think I'm starting to understand. Well, for now I've used a scripted alternative. Using a value that I clamp between wanted angle, and set this value to the rotation. Ins$$anonymous$$d of reading the rotation from the engine, so far it works well.

But I'll keep in $$anonymous$$d your way If I run into future blocks and gonna do some research on quaternitions.

Thanks.

avatar image Cyb3rManiak · Oct 20, 2010 at 10:34 AM 0
Share

@Jesse - Thanks, I see your point now, and I've tried editing my answer to make it clearer if anyone sees this post later on...

avatar image
1

Answer by Adam Rademacher · Oct 18, 2010 at 10:21 PM

This has to do with the way unity converts its native rotations (Quaternions) to Euler Angles (Vector3). If you look at the transform in the inspector, the Y and Z rotations are changing when you hit certain points to compensate for the conversion. If I were you, I'd look into having a function that relies on Mathf.Angle and not Quaternion to Euler conversion (mostly because I am afraid of Quaternions)

Comment
Add comment · Show 2 · 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 Oninji · Oct 18, 2010 at 10:30 PM 0
Share

Well, when on another object I use this on the Y angle, and there's no error with the Y rotation. It's only the X rotation.

I'm currently working on a way to make rotation dependant of a variable as an alternative, and so I could check on the Variable ins$$anonymous$$d.

$$anonymous$$eanwhile, I'll also check if there's an alternative using $$anonymous$$athf, but I'm not sure, but well it's worth a shot.

If I could find a way to get reading of the object "True Rotation Angle" it would be truly the most simple way.

avatar image Jesse Anders · Oct 19, 2010 at 05:53 AM 0
Share

It's neither a bug nor an error. Also, there isn't a 'true rotation angle' as far as Euler angles are concerned. If you want the pitch/elevation angle to be computed in a certain way though, you can store and update the rotation angles yourself and then build the orientation directly from them, or you can compute the angles from the direction vectors using trig,

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

No one has followed this question yet.

Related Questions

Problem with Z rotation when rotating X and Y and the same time. 2 Answers

Character Rotation - Script not functioning correctly 1 Answer

How do i make a flowing 90 degree turn? 1 Answer

Rotate character to the moving direction problems? 2 Answers

Rotating sprite through touch (storing current rotation) 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