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
0
Question by efealacamli · Mar 03, 2013 at 05:26 AM · rotationquaternionlerprubikscube

Rotating the cube with Quaternion.Lerp

Hi. I'm working on the rotation of the rubik's cube and having some problems. The first rotation always rotates correctly(90 degrees). However, the rotations coming after the first one are not working correctly; rotates 180 degrees when I try to rotate the cube opposite way as the first rotate, or rotates on two axis when I try to rotate the cube in a different axis as the first rotate.

After the mouse click, I determine the rotation of the cube with FindRotationOfCube(). Then I calculate the direction of the rotation depends on the Mouse movement in RotateDirection(). Last, I rotate the cube with Quaternion.Lerp in RotateSlice().

Here's my code.

     private void Update()
     {
         switch(stateRotate)
         {
             case "WaitForMouseInput":
                 WaitForMouseInput();
                 break;
 
             case "FindRotationOfCube":
                 FindRotationOfCube();
                 break;
 
             case "RotateDirection":
                 RotateDirection();
                 break;
 
             case "RotateSlice":
                 RotateSlice();
                 break;
         }
     }
   
     void WaitForMouseInput()
     {
         if(mScript.canRotate)
         {
             stateRotate = "FindRotationOfCube";
         }
     }
   
     void FindRotationOfCube()
     {
         fromRotation = cubie5.transform.rotation;
         rotX = cubie5.transform.rotation.x;
         rotY = cubie5.transform.rotation.y;
         rotZ = cubie5.transform.rotation.z;
 
         stateRotate = "RotateDirection";
     }
     
     void RotateDirection()
     {
         if(mScript.mouseMovedDir == "Left")
         {
             toRotation = Quaternion.Euler(rotX, rotY + 90, rotZ);
         }
         else if(mScript.mouseMovedDir == "Right")
         {
             toRotation = Quaternion.Euler(rotX, rotY-90, rotZ);
         }
         else if (mScript.mouseMovedDir == "Up")
         {
             toRotation = Quaternion.Euler(rotX, rotY, rotZ - 90);
         }
         else if (mScript.mouseMovedDir == "Down")
         {
             toRotation = Quaternion.Euler(rotX, rotY, rotZ + 90);
         }
 
         stateRotate = "RotateSlice";
     }

     void RotateSlice()
     {
         cubie5.transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * 10);
         stateRotate = "WaitForMouseInput";
     }


Have any suggestions? Thanks.

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 Chronos-L · Mar 03, 2013 at 05:33 AM 0
Share

It will be helpful if you can post more codes, and perhaps provide us with screenshots and what are the steps done to produce the result in the screenshots.

At least for me, I am having trouble trying to visualize it.

avatar image Tarlius · Mar 03, 2013 at 05:53 AM 0
Share

Are you updating rotX/rotY/rotZ before setting toRotation?

1 Reply

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

Answer by robertbu · Mar 03, 2013 at 08:22 AM

Here's a solution. A bit ugly, and I am a bit frustrated that I could not figure out the qTo rotation using Quaternion methods rather than RotateAround(). You can replace the RotateTowards() with Lerp() (since that was your question) if you like, but I liked the feel of RotateTowards() better:

 public class RotationsBy90 : MonoBehaviour {
     
     public float speed = 55.0f;
     private Quaternion qTo = Quaternion.identity;
     private float treshold = 1.0f;
     
     void Update () {
         if (Quaternion.Angle (transform.rotation, qTo) > treshold) {
             transform.rotation = Quaternion.RotateTowards (transform.rotation, qTo, Time.deltaTime * speed);
             
         }
         else {
             transform.rotation = qTo;
             Quaternion qSave;
             if (Input.GetKeyDown (KeyCode.UpArrow)) {
                 qSave = transform.rotation;
                 transform.RotateAround (transform.position, Vector3.right, 90.0f);
                 qTo = transform.rotation;
                 transform.rotation = qSave;
             }
             else if (Input.GetKeyDown (KeyCode.DownArrow)) {
                 qSave = transform.rotation;
                 transform.RotateAround (transform.position, Vector3.left, 90.0f);
                 qTo = transform.rotation;
                 transform.rotation = qSave;                
             }
             else if (Input.GetKeyDown(KeyCode.LeftArrow)) {
                 qSave = transform.rotation;
                 transform.RotateAround (transform.position, Vector3.up, 90.0f);
                 qTo = transform.rotation;
                 transform.rotation = qSave;            
             }
             else if (Input.GetKeyDown (KeyCode.RightArrow)) {
                 qSave = transform.rotation;
                 transform.RotateAround (transform.position, Vector3.down, 90.0f);
                 qTo = transform.rotation;
                 transform.rotation = qSave;            
             }
         }
     
     }
 }
Comment
Add comment · Show 3 · 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 efealacamli · Mar 03, 2013 at 06:43 PM 0
Share

Thanks for the solution. It works perfectly. Also, using RotateTowards() looks better than using Lerp(). I couldn't understand the logic about using qSave. Could you explain the code after we get input from keyboard?

avatar image robertbu · Mar 03, 2013 at 07:16 PM 0
Share

transform.RotateAround() is a rotation relative to the current rotation, but it happens immediately. So what I'm doing is saving the current rotation, doing an immediate (but relative) 90 degree rotation to generate the rotation that I want to use as qTo, and then restoring the original rotation. If I did not save and restore the rotation, you would see an immediate rotation rather than a rotation over time. I really don't like doing it way, but by quick try with using the Quaternion class directly failed. Quaternion methods are all absolute rotations rather than relative rotations.

avatar image efealacamli · Mar 03, 2013 at 07:23 PM 0
Share

I get it now. Thanks for the detailed explanation!

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

11 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

Related Questions

Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 Answers

Clamped Turret Doesn't Want to Lerp the Other Way 2 Answers

Unity Quaternion Problem (Should be A Bug?) 1 Answer

How to smoothly roate a cube by 90 degrees. 2 Answers

Rotate multiple objects around a object, maintaining their own trajectory(not rotating their local forward vector) 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