• 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
Question by TaylorAnderson · Sep 17, 2017 at 02:12 AM · rotationquaternioncubeeulerangleseuler

Trying to tween the rotation of a cube in x and z and having trouble

I'm trying to make a first person game where, on a very basic level, if the player looks at a cube and hits a button, the cube rotates away from the player 90 degrees. The cube and the player are sitting on a floor, so the y axis isnt a factor, just x and z. But it seems like no matter what methods I try, the best case scenario is that it works, but it gets really strange when you rotate one axis and then the other, or rotate the x axis beyond 360 degrees. Is there a way to fix this? I've tinkered with Quaternions and I have a feeling that's probably the answer, but I'm not super familiar with them, so that method hasnt been going well either.

Any help would be greatly appreciated.

-Taylor Anderson

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by TaylorAnderson · Sep 17, 2017 at 11:50 PM

Okay, I've solved this. For whoever reads this thread in the future, you may find this helpful.

Code:

         MidiOut.SetInstrument(instrument, channel);
         MidiOut.NoteOn(noteIndex, 100, channel.ToInt());
         if (lastRotation != null) transform.rotation = lastRotation;
         Quaternion rotation;
         if (Mathf.Abs(transform.position.x - arm.transform.position.x) > Mathf.Abs(transform.position.z - arm.transform.position.z)) {
             if (transform.position.x < arm.transform.position.x) {
                 rotation = Quaternion.Euler(0, 0, 90);
             }
             else {
                 rotation = Quaternion.Euler(0, 0, -90);
             }
 
         }
 
         else {
             if (transform.position.z < arm.transform.position.z) {
                 rotation = Quaternion.Euler(-90, 0, 0);
             }
             else {
                 rotation = Quaternion.Euler(90, 0, 0);
             }
         }
         rotation *= transform.rotation;
         lastRotation = rotation;
         transform.DORotateQuaternion(rotation, 0.4f).SetEase(Ease.OutBack);


I've added a few extra things, but the core is essentially the same. What changed was: instead of initially setting rotation to be transform.rotation, and then multiplying it by the rotation I actually wanted, I did it in reverse. I set the rotation variable to the desired rotation, and then at the very end I applied it to transform.rotation.

Doing the multiplication in the reverse order like this has the effect of making the rotation in WORLD space, not LOCAL space, so the object's previous rotation doesn't have an effect on its future rotations.

Thanks to @kryzodoze for sending me in the right direction.

Comment
kryzodoze

People who like this

1 Show 0 · 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

Answer by kryzodoze · Sep 17, 2017 at 04:08 AM

But it seems like no matter what methods I try, the best case scenario is that it works, but it gets really strange when you rotate one axis and then the other, or rotate the x axis beyond 360 degrees.

If you rotate one axis, rotating another axis will always be relative to the new rotation. Try rotating the x-axis 90 degrees in the editor, and then applying your y or z rotation. Is it what you want? If yes, then you should be fine in the editor.

Rotating beyond 360 degrees should be fine, I believe Unity just mods it with 360 and uses the new value.

Can you explain a little more what exactly you are doing and seeing?

Comment

People who like this

0 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 TaylorAnderson · Sep 17, 2017 at 02:41 PM 0
Share

Here is the code I am using for this (forget the stuff about Midi)

     public void OnHit(ArmBase arm) {
         //text.enabled = true;
         MidiOut.SetInstrument(instrument, channel);
         MidiOut.NoteOn(noteIndex, 100, channel.ToInt());
 
         var rotation = transform.rotation;
         //if we're more aligned on the x axis, rotate along that axis
         if (Mathf.Abs(transform.position.x - arm.transform.position.x) < Mathf.Abs(transform.position.z - arm.transform.position.z)) {
             print("Aligned along x");
             if (transform.position.x < arm.transform.position.x) {
                 rotation *= Quaternion.Euler(90, 0, 0);
             }
             else {
                 rotation *= Quaternion.Euler(-90, 0, 0);
             }
         }
         //if we're more aligned on z axis, rotate along that axis.
         else {
             print("Aligned along z");
             if (transform.position.z < arm.transform.position.z) {
                 rotation *= Quaternion.Euler(0, 0, -90);
             }
             else {
                 rotation *= Quaternion.Euler(0, 0, 90);
             }
         }
         StartCoroutine(TweenRotate(rotation));
     }
     private IEnumerator TweenRotate(Quaternion rotation) {
         for (float t = 0; t <= 1; t+=0.05f) {
             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, t);
             yield return new WaitForFixedUpdate();
         }
     }


I think you're definitely right, the problem is related to the fact that rotating along one axis and then along another doesnt quite work as I expected-- but I'm not sure how to adjust my rotation to account for the object's current rotation.

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

97 People are following this question.

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

Related Questions

Can't Accurately Read Rotations 0 Answers

Is there no way to get reliable angles from a rigidbody? 2 Answers

Rotate a cube by 90 degree 1 Answer

Need help with storing Rotation in a Quaternion 1 Answer

Fixing the rotation around a particular axis using Quaternions? 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