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 Kalu · Nov 15, 2011 at 08:18 PM · rotationquaternionsmooth

Quaternion, Smooth cube rotation using "wheel"

I have this piece of code:

 private float curDifference = 0;
 private float curApplyDifference = 1;
 void onMouseRotateDrag (GameObject curPlanche)
 {
     float posInitX = initMousePosX;//position du curseur au moment du clic
     float posInitY = initMousePosY;
     float posCurX = Input.mousePosition.x;//position du curseur pendant le drag
     float posCurY = Input.mousePosition.y;
 
     if(posInitX-posCurX<0 || posInitY-posCurY<0){//floor() valeur inf et ceil() valeur sup
        curDifference = Mathf.Floor((posCurX-posInitX)/100);
        if(curDifference <= 0){
          curDifference = Mathf.Floor((posCurY-posInitY)/100);
        }
 
        if(curTarget.name == "Red"){ // axe X
          if(curDifference != curApplyDifference && curDifference>0){
           Quaternion curPlanchInitRot = curPlanche.transform.localRotation;//pos d'origine
           Quaternion curPlanchTargetRot = curPlanche.transform.localRotation;//pos d'origine puis modifiée
           curPlanchTargetRot[0] += (45*curDifference);
           curPlanche.transform.localRotation = Quaternion.Lerp(curPlanchInitRot, curPlanchTargetRot, 1.5f);
           Debug.Log(curPlanchInitRot+"             "+curPlanchTargetRot);
           curApplyDifference = curDifference;
          }
        }
        if(curTarget.name == "Green"){ // axe Y
 
        }
        if(curTarget.name == "Blue"){ // axe Z
 
        }
     }
 }

The function executes when the mouse moves .. (after a click on one of the "wheels" of rotation)

I use a set of three rollers to apply the rotations to the cube (on the model of the tool rotation 3DS Max). I made "debug.log" to the right, left, and the results seems correct but the cube does not move. I continued to search ..

Comment
Add comment
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
1
Best Answer

Answer by aldonaletto · Nov 15, 2011 at 08:31 PM

This is wrong:

       curPlanchTargetRot[0] += (45*curDifference);

The quaternion components have nothing to do with the familiar x, y and z degree angles we see in the Inspector - these are the eulerAngles:

       curPlanchTargetRot.eulerAngles.x += (45*curDifference);



Comment
Add comment · Show 6 · 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 Kalu · Nov 15, 2011 at 08:44 PM 0
Share

float x = (45*curDifference); curPlanchTargetRot.eulerAngles += new Vector3(x, 0, 0);

thx for eulerAngles, but not yet "smooth" rotation, any idea?

edit: sorry i use C#

avatar image aldonaletto · Nov 15, 2011 at 08:59 PM 0
Share

The Lerp/Slerp functions are the usual way to smooth things. Change the Lerp line to:

     curPlanche.transform.localRotation = Quaternion.Slerp(curPlanche.transform.localRotation, curPlanchTargetRot, 1.5f*Time.deltaTime);

This will work if on$$anonymous$$ouseRotateDrag() is called in Update or in a coroutine.
NOTE: I'm supposing the rest of your script is ok.

avatar image Kalu · Nov 15, 2011 at 09:20 PM 0
Share

I move a piece of code like this in the Update:

if (switchControl == true) {//rotation controler & one of the axe's selectionned if (curTarget.name == "Red" | | curTarget.name == "Green" | | curTarget.name == "Blue") { curPlanche.transform.localRotation Quaternion.Slerp = (curPlanche.transform.localRotation curPlanchTargetRot, 1.5f * Time.deltaTime); } }

In order to rotate the enterFrame, changing variables in a function remained running as soon as the mouse moves (after you click on one of the axes) But all I get is a slight tremor that (emphasis) ended up gently rotate the cube, the function does not run properly. It is one way then the other ..

avatar image Kalu · Nov 15, 2011 at 09:24 PM 0
Share

I'll try with a coroutine.. but tomorrow. Thx for ur help !

avatar image aldonaletto · Nov 15, 2011 at 09:39 PM 0
Share

Using Update is easier - coroutines give a lot of headaches. I think you could change your logic to this:
1- have the current angles in a Vector3 variable:

  Vector3 angles = Vector3.zero;

2- change angles.x, angles.y or angles.z according to the mouse:

  angles.x = (angles.x + variation) % 360; // keep angle in modulo 360

3- let Slerp follow the angles smoothly in Update:

  void Update(){
      curPlanchTargetRot = Quaternion.Euler(angles);
      curPlanche.transform.localRotation = Quaternion.Slerp(curPlanche.transform.localRotation, curPlanchTargetRot, 1.5f*Time.deltaTime);
  }
Show more comments
avatar image
0

Answer by Kalu · Nov 18, 2011 at 05:08 PM

I have this code wich rotate a cube:

 private float curDifference = 0;
 private float differenceY = 0;
 private float differenceX = 0;
 private float curApplyDifference = 1;
     
     void onMouseRotateDrag (GameObject curPlanche)
         {
             float posInitX = initMousePosX;//position du curseur au moment du clic
             float posInitY = initMousePosY;
             float posCurX = Input.mousePosition.x;//position du curseur pendant le drag
             float posCurY = Input.mousePosition.y;
             
             differenceX = Mathf.Floor((posCurX-posInitX)/150);
             differenceY = Mathf.Floor((posCurY-posInitY)/150);
             curDifference = differenceX;
             if(Mathf.Abs(curDifference)<Mathf.Abs(differenceY)){//floor() valeur inf et ceil() valeur sup
             //    curDifference = differenceY;
             }
             
             if(curDifference != curApplyDifference){
                 if(curTarget.name == "RedR"){ // axe X
                     angles.x = ((-curDifference*90) % 360);
                     curApplyDifference = curDifference;
                 }else if(curTarget.name == "GreenR"){ // axe Y
                     angles.y = ((-curDifference*90) % 360);
                     curApplyDifference = curDifference;
                 }else if(curTarget.name == "BlueR"){ // axe Z
                     angles.z = ((-curDifference*90) % 360);
                     curApplyDifference = curDifference;
                 }
                 posInitX = Input.mousePosition.x;
                 posInitY = Input.mousePosition.y;
             }
         }
     void Update () {
             if(switchControl == true && curTarget != null){
                 if(curTarget.name == "RedR" || curTarget.name == "GreenR" || curTarget.name == "BlueR"){
                     Quaternion curPlanchTargetRot = Quaternion.Euler(angles);
                      curPlanche.transform.rotation = Quaternion.Slerp(curPlanche.transform.rotation, curPlanchTargetRot, 10f*Time.deltaTime);    
                      repereRotation.transform.rotation = Quaternion.Slerp(repereRotation.transform.rotation, curPlanchTargetRot, 10f*Time.deltaTime);    
                 }
             }
     }

but when I apply a series of rotation, they end up not match with the selected axis. So I run the code by selecting the cube and rotation values ​​in x, y and z exchange in strange ways. While I can see visually a rotation only along the X axis, the values ​​X, Y and Z of rotation are changing ... I don't really understand why ... (i also try with localRotation..) or rather how to fix this problem..

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Smooth reset rotation problem 1 Answer

Choppy rotation of character around y-axis 1 Answer

Rotation - Simple Question 0 Answers

smooth look at with offset 0 Answers

Quaternion Rotation Smooth 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