Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by zoggzter · Apr 26, 2016 at 08:00 PM · transformquaterniontransform.rotationquaternions

Rotating transform more than 180 degrees

Hi, I was wondering why my transform won't keep rotating after 180 degrees. The rotation must be set and not something like Transform.Rotate due to the circumstances in my game.

Here is the code: using UnityEngine; using System.Collections;

 public class Test : MonoBehaviour {
 
     public float speed = 10;
     public float time = 0.25f;
 
     void Start () {
 
         StartCoroutine("Wait");
 
     }
 
     void Rotate () {
 
         Vector3 rot = transform.rotation.eulerAngles;
         rot += new Vector3(speed, 0, 0);
         transform.rotation = Quaternion.Euler(rot);
         StartCoroutine("Wait");
 
     }
 
     IEnumerator Wait (){
 
         yield return new WaitForSeconds (time);
         Rotate ();
 
     }
 }


Here is the result: https://gyazo.com/bd66665ac525b74afab5879c1a5d1a1a

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 incorrect · Apr 26, 2016 at 11:19 PM 1
Share

You can rotate objects using quaternions ins$$anonymous$$d of euler angles. To rotate an object you just need to multiply it's rotation (which is a quaternion) by a quaternion representing rotation.

Something like that:

 transform.rotation = transform.rotation * Quaternion.AngleAxis(5f, Vector3.up);
avatar image zoggzter incorrect · Apr 27, 2016 at 05:42 AM 0
Share

Thank you very much, this solution worked

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_bTkJGG31azdikQ · Jan 23, 2020 at 06:51 PM

I know this is four years old, but for posterity.

Converting from euler angle to quaternion and back won't always get you what you started with, since there's more than one way to represent a rotation with euler angles (well, with quaternions too). When I tested this code, it actually wouldn't rotate more than one step past 90 degrees. After setting rot to (100, 0, 0), storing it as a quaternion and pulling it back into an euler representation, it turns out to be (80, 180, 180), and when you add 10 degrees to the x component of that, you move backward. Unity stores the quaternion representation and just gives the euler representation when it's asked for. In general, euler angles should only be used for humans to input and read rotations/orientations.

I don't understand why Transform.Rotate won't work for you, but if you must avoid it, you might try storing the euler representation as a class variable to ensure it stays as expected. The downside here is that if you're rotating around multiple axes, you might also get unexpected results because euler rotations mean rotations around specific axes in a particular order. If you're just doing one axis for a given object, you should be fine, though.

Comment
Add comment · 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
0

Answer by DSivtsov · Aug 24, 2021 at 10:05 PM

Work Variant will be

 void Rotate()
 {
     Quaternion deltaQuaternionRot = Quaternion.AngleAxis(speed, Vector3.right);
     transform.rotation = transform.rotation * deltaQuaternionRot;
     StartCoroutine("Wait");
 }

I'm no big specialist in Rotation in Unity, but:

  1. Rotation in Unity based on Quaternion mathematics where "rotation" = "Quaternion * Quaternion";

  2. the rot += new Vector3(speed, 0, 0); is wrong because you simply summarize two Vectors (Vector3), that rot = transform.rotation.eulerAngles doesn't matter, because Vector3 is just a container for storing three unrelated numbers

P.S> Quaternion.Euler(new Vector3(100, 0, 0)) == Quaternion.Euler(new Vector3(80,180, 180)) but (Quaternion.Euler(new Vector3(100, 0, 0))).eulerAngles == new Vector3(80,180, 180). This is the specificity of the Quaternion to Euler conversion algorithm. ( not only Unity). I saw recommendation "Be careful with Get "Quaternion.eulerAngles()"

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Euler Angles in code / inspector different (both local & global) 1 Answer

What is the difference between setting transform.eulerAngles and transform.rotation? 1 Answer

transform rotations overwriting eachother 1 Answer

I cant find the issue on my code (Quaternion.Slerp) 1 Answer

Why does assigning a quaternion to transform rotation work but calling transform.rotation. doesnt? 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