Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Quaternion Angles messed up

Discussion in 'Scripting' started by asd234w4r5, May 18, 2018 at 3:54 PM.

  1. asd234w4r5

    asd234w4r5

    Joined:
    Oct 4, 2012
    Posts:
    506
    Hello everyone!

    I am trying to rotate an object around its X-Axis and make it stop at certain points. So lets say, there is a sentence with 5 words, these 5 words get wrapped around this capsule and you can later select them. My code for the rotation is here, while the important part is the last one where the angle gets calculated by the "steps"

    Code (CSharp):
    1.  
    2. float steps = 360 / Bandit_Game.gameBandit.answerArray.Length;
    3. //The answers are the split up text, like 5 words
    4.  
    5. private void Update()
    6.     {
    7.         RaycastHit hit;
    8.         Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
    9.  
    10.         if (Physics.Raycast(ray, out hit))
    11.         {
    12.             if (hit.collider.GetComponent<CapsuleCollider>().transform.IsChildOf(transform))
    13.             {
    14.                 cardSelected = true;
    15.             }else{
    16.                 cardSelected = false;
    17.             }
    18.         }
    19.  
    20.         if (Input.GetMouseButtonDown(0) && cardSelected)
    21.         {
    22.             differenceX = 0.0f;
    23.         }
    24.         else if (Input.GetMouseButton(0) && cardSelected)
    25.         {
    26.             differenceX = Mathf.Abs(lastX - Input.GetAxis("Mouse Y"));
    27.  
    28.             if (lastX > Input.GetAxis("Mouse Y"))
    29.             {
    30.                 direction = -1;
    31.                 transform.Rotate(Vector3.right, -differenceX);
    32.             }
    33.  
    34.             if (lastX < Input.GetAxis("Mouse Y"))
    35.             {
    36.                 direction = 1;
    37.                 transform.Rotate(Vector3.right, differenceX);
    38.             }
    39.  
    40.             lastX = -Input.GetAxis("Mouse Y");
    41.         }
    42.         else
    43.         {
    44.             if (differenceX > 0.5f)
    45.             {
    46.                 differenceX -= 0.1f;
    47.                 transform.Rotate(Vector3.right, differenceX * direction);
    48.             }
    49.             else if (differenceX < -0.5f)
    50.             {
    51.                 differenceX += 0.1f;
    52.                 transform.Rotate(Vector3.right, differenceX * direction);
    53.             }
    54.             else
    55.             {
    56.                 float vecX = Mathf.Round(transform.eulerAngles.x / steps) * steps;
    57.  
    58.                 transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(new Vector3(vecX, transform.eulerAngles.y, transform.eulerAngles.z)), Time.deltaTime * 50f);
    59.             }
    60.         }
    61.  
    62.         Debug.Log(transform.rotation.x);
    63.     }
    What is happening now: I have 5 * 72 degree steps, which work on the Quaternion between 0 and 1, but as soon as I turn the rotation over 180 degrees, it will start to like "mirror" the positions and it stops between those points instead. I hope you understand, what I mean, otherwise just ask and I get more in detail.

    At least, what I wanted to achieve and couldn't figure out is
    • Calculate the steps (360 / words)
    • Generate the words around the capsule with their right rotation (this is working)
    • Snap the rotation to the nearest "step" calculated in the first bullet

    I hope, you can help me, figure it out. I was trying to work in quaternion world only, but this is unreliable, going through eulerAngles didn't do the trick neither for now.

    Thank you!