Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object rotation help

Discussion in 'Scripting' started by vyruz18, Dec 23, 2015.

  1. vyruz18

    vyruz18

    Joined:
    May 27, 2013
    Posts:
    4
    Hello,

    I'm working with a friend on an application to virtually dissect a 3D model of a human hand. Ive got a simple question about object rotation, I allready spent quite some time on google for an answer but regrettably.



    this is how our current rotation works
    we are using this script:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var xSpeed = 10;
    4. var ySpeed = 10;
    5. private var x = 0.0;
    6. private var y = 0.0;
    7.  
    8.  
    9. function Start ()
    10. {
    11.     var angles = transform.eulerAngles;
    12.     x = angles.y;
    13.     y = angles.x;
    14.     // Make the rigid body not change rotation
    15.     if (GetComponent.<Rigidbody>())
    16.         GetComponent.<Rigidbody>().freezeRotation = true;
    17. //    var VolumeSliderGet = GameObject.Find("Main Menu/Panel/Slider").GetComponent<Slider>().value;
    18.  
    19. }
    20.  
    21. function Update () {
    22.  
    23.     var muscleScrollView = GameObject.Find("Main Menu/Panel/Home/HandStuctures/Muscles/MuscleScrollView");
    24.     var boneScrollView = GameObject.Find("Main Menu/Panel/Home/HandStuctures/Bones/BoneScrollView");
    25.     var nerveScrollView = GameObject.Find("Main Menu/Panel/Home/HandStuctures/Nerves/NerveScrollView");
    26.     var arteryScrollView = GameObject.Find("Main Menu/Panel/Home/HandStuctures/Arteries/ArteryScrollView");
    27.     var ligamentScrollView = GameObject.Find("Main Menu/Panel/Home/HandStuctures/Ligaments/LigamentScrollView");
    28.     var wordScrollView = GameObject.Find("Main Menu/Panel/Zoeken/InputFieldModified/WordList");
    29.     var slider = GameObject.Find("Main Menu/Panel/Slider/SliderHelper");
    30.  
    31.    
    32.      if(muscleScrollView.activeInHierarchy == false && boneScrollView.activeInHierarchy == false && nerveScrollView.activeInHierarchy == false
    33.         && arteryScrollView.activeInHierarchy == false && ligamentScrollView.activeInHierarchy == false && wordScrollView.activeInHierarchy == false) //&& slider.activeInHierarchy == true)
    34.     {
    35.         if (Input.GetMouseButton(0))
    36.         {
    37.             x += Input.GetAxis("Mouse X") * xSpeed * 0.005;
    38.             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.005;
    39.      
    40.         }
    41.      
    42.         var rotation = Quaternion.Euler(y, x, 0);
    43.         var rotation2 = Quaternion.Euler(-y, x, 0);  
    44.            var object = GameObject.Find("hand");
    45.  
    46.          transform.rotation = rotation;
    47.     }
    48.    
    49.  
    50. }
    51.  
    However, we would like to get a rotation something more like you can see here:
    http://www.wowhead.com/dressing-room#mz0z0zH89s8pZk808pZs87spZa808pZo808pZc808pZR808pZm87V

    I believe its due to the fact that we arent rotation around the objects center point, right now the rotation on the Y-axis gets inverted when you look at the model from behind which isnt what we want.

    Hopefully this is enough information of our problem, any help/suggestions is welcome!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Euler angles don't give you enough control for my tastes, and if the rotations are applied in the wrong order, you can get odd effects like this. In this case you want to rotate around the vertical then around the horizontal. You can probably solve it by using multiple Quaterion.AngleAxis calls:
    Code (csharp):
    1. var hRotation = Quaternion.AngleAxis(x, Vector3.up);
    2. var rotation = hRotation * Quaternion.AngleAxis(y, hRotation * Vector3.right);
    The * operator, for Quaternions, basically applies the rotation to whatever comes after, including other Quaternions; or if it's a Vector3, it rotates that vector.

    The second AngleAxis call is more complex because it needs to know what the current "right" vector is in order to rotate around it as an axis.
     
  3. vyruz18

    vyruz18

    Joined:
    May 27, 2013
    Posts:
    4
    Thanks alot for your input!
    I tried your suggestion, I think it brings me a little bit closer to what we are trying to achieve, but its not there yet.

    To clarify things a bit more I made 2 simple videos, where I replaced our handmodel with a cube:

    the original script:



    the rotation is working fine at the beginning, but when you turn the cube for 90 degrees things go wrong.

    The adapted script with StarManta's suggestions:



    when you spin the cube 90 degrees, it inverts the Y-axis rotation, when you rotate it 90 degrees (so 180 degrees from starting) its restored again.

    What we are trying to achieve: if you drag the left mousebutton from top to bottom, no matter in what angle the object stands, the rotation on the Y-axis should be the same (like in that World of Warcraft dressing room link I pasted in my first link).I'm not an expert with Quaternions, I do understand that they give better results than Euler angels, but maybe I'm making a very silly mistake somewhere in the script:


    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var xSpeed = 10;
    4. var ySpeed = 10;
    5. private var x = 0.0;
    6. private var y = 0.0;
    7.  
    8.  
    9. function Start ()
    10. {
    11.     var angles = transform.eulerAngles;
    12.     x = angles.y;
    13.     y = angles.x;
    14.  
    15.     if (GetComponent.<Rigidbody>())
    16.         GetComponent.<Rigidbody>().freezeRotation = true;
    17.  
    18. }
    19.  
    20. function Update () {
    21.  
    22.     var muscleScrollView = GameObject.Find("Main Menu/Panel/Home/HandStuctures/Muscles/MuscleScrollView");
    23.     var boneScrollView = GameObject.Find("Main Menu/Panel/Home/HandStuctures/Bones/BoneScrollView");
    24.     var nerveScrollView = GameObject.Find("Main Menu/Panel/Home/HandStuctures/Nerves/NerveScrollView");
    25.     var arteryScrollView = GameObject.Find("Main Menu/Panel/Home/HandStuctures/Arteries/ArteryScrollView");
    26.     var ligamentScrollView = GameObject.Find("Main Menu/Panel/Home/HandStuctures/Ligaments/LigamentScrollView");
    27.     var wordScrollView = GameObject.Find("Main Menu/Panel/Zoeken/InputFieldModified/WordList");
    28.     var slider = GameObject.Find("Main Menu/Panel/Slider/SliderHelper");
    29.  
    30.  
    31.      if(muscleScrollView.activeInHierarchy == false && boneScrollView.activeInHierarchy == false && nerveScrollView.activeInHierarchy == false
    32.         && arteryScrollView.activeInHierarchy == false && ligamentScrollView.activeInHierarchy == false && wordScrollView.activeInHierarchy == false) //&& slider.activeInHierarchy == true)
    33.     {
    34.         if (Input.GetMouseButton(0))
    35.         {
    36.             x += Input.GetAxis("Mouse X") * xSpeed * 0.005;
    37.             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.005;
    38.    
    39.         }
    40.    
    41.         var hRotation = Quaternion.AngleAxis(-x, Vector3.up);
    42.         var rotation = hRotation * Quaternion.AngleAxis(y, hRotation * Vector3.right);
    43.  
    44.        // var rotation = Quaternion.Euler(y, x, 0);
    45.      //   var rotation2 = Quaternion.Euler(y, x, 0);
    46.     //       var object = GameObject.Find("hand");
    47. //           Debug.Log(object.transform.rotation.x);
    48. //           Debug.Log(object.transform.rotation.y);
    49.  
    50.                transform.rotation = rotation;
    51.  
    52.      }
    53.  
    54.  
    55. }
    56.  
     
  4. vyruz18

    vyruz18

    Joined:
    May 27, 2013
    Posts:
    4
    I managed to solve this myself for now, I merged a few things from other scripts and it did the trick. At the end it was just a few lines that needed to get adjusted in the script:

    Code (JavaScript):
    1.  if (Input.GetMouseButton(0))
    2.         {
    3.             transform.Rotate((-Input.GetAxis("Mouse Y") * RotationSpeed * Time.deltaTime), (Input.GetAxis("Mouse X") * RotationSpeed * Time.deltaTime), 0, Space.World);



    I think I still need to finetune it a bit more, but its rotating how I want it to rotate for now :)