• 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 HROP · Jan 09, 2012 at 04:19 AM · modelglitcheulerangles

Problem with euler angles, randomly spinning around axis

Hi,

I've got a model, that has a script attached to it, three thing are going on.

 transform.eulerAngles.z = 23;
 transform.position.x = jointb.transform.position.x;
 transform.position.y = jointb.transform.position.y;
 
 Debug.Log (transform.localEulerAngles.z);

I am basically parenting the model manually to an other Game Object, that is defined in a function Start. The problem I have is that whenever I try to change the euler Angles or the local Euler Angles, the model starts spinning like crazy around the world y axis, no matter what number I write.

I've tried everything I could think of. And instead of seting the angle value, it's as if it was incrementing the value every cycle.

The reason I am using this instead of rotate, is because my goal is to set the .z angle using (Mathf.Atan2(jointp.transform.position.y, jointb.transform.position.x) *Mathf.Rad2Deg)

Before I imported this model, it worked fine with a placeholder. Any help is welcome.

Comment
Add comment · Show 3
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 asafsitner · Jan 09, 2012 at 10:32 AM 0
Share

Double-check the pivots, rotations, and scale of the parent object / imported model. Also notice you're moving it in global space but printing it's local space rotation.

avatar image Kryptos · Jan 10, 2012 at 10:35 AM 0
Share

$$anonymous$$ight be related to gimbal-lock problems.

Try using Quaternions ins$$anonymous$$d of modifying euler angles. Quaternion.AngleAxis() might be a good start.

avatar image HROP · Jan 11, 2012 at 02:37 PM 0
Share

Hi,

Thanks for your suggestions. Yes I have double-checked everything, the reason I am printing the local, is because, previously, in the code, I have tried modifying the local euler angles. But that didn't work either.

I finally bypassed the problem by using quaternions directly, the following way: targetRot = Quaternion.LookRotation(jointpiston.transform.position - transform.position); targetRot *= Quaternion.Euler(90,0,0); transform.rotation.z = (targetRot.z);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Jan 10, 2012 at 02:54 PM

The code above contains a sin: it violates the commandment "Thou shall not modify a single eulerAngles axis".
Despite the docs say eulerAngles is a Vector3 variable, it's not: it's actually a property - its getter routine converts the quaternion Transform.rotation to a 3-axes format and return it as a Vector3 structure. But 3-axes is a redundant way to define a rotation: there are several combinations that give the same result - (0, 180, 0) and (180, 0, 180) produce the same rotation, for instance. Thus, when converting from a quaternion (which is a single rotation around an arbitrary axis) to 3-axes, the routine must decide which combination to return - and many times it's not the one we want.
When you write this:

 transform.eulerAngles.z = 23;

the actual code generated is something like this (fictitious function names):

 var temp: Vector3 = ConvertQuaternionToEuler(transform.rotation); 
 temp.z = 23;
 transform.rotation = ConvertEulerToQuaternion(temp);

The problem is in the first line: the values returned by the function may not be what you expect. As in the example, it could return (180, 0, 180) or (0, 180, 0) for a given rotation - and modifying z alone produces different results: (180, 0, 23) is not the same thing as (0, 180, 23) - one is upside down relative to the other, to be more precise.
To avoid this problem, you should use:

 transform.eulerAngles = Vector3(0, 0, 23);

The x and y values in this case are 0, but they may be any valid angle - except eulerAngles.x and eulerAngles.y, of course!.
NOTE: The eulerAngles.z thing is wrong, for sure, but doesn't seem to be enough to make the object spin continuously; maybe something else in your code is adding to the eulerAngles problem to produce this effect.

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 HROP · Jan 11, 2012 at 02:40 PM 0
Share

Hi thanks for the explanations. Indeed I ended up using quaternions for this little script.

I do not know what had caused the problem. However, the script works better that way, than it did when i was using the euler angles. (because for some reason the script worked fine when using a place-holder, but began its weird behaviour as soon as I added a model).

Here's the solution, inspired from somebody else who's had a similar problem. targetRot = Quaternion.LookRotation(jointpiston.transform.position - transform.position); targetRot *= Quaternion.Euler(90,0,0); transform.rotation.z = (targetRot.z);

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

7 People are following this question.

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

Related Questions

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

Texture Glitch? 1 Answer

How to import model without it glitching? 1 Answer

Weird lighting bug on model. 0 Answers

Blender and Unity won't play nice. Missing faces and weird render glitches 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