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

[SOLVED] Changing X rotation changes Y euler angles.

Discussion in 'Scripting' started by Matrix505, Jan 6, 2020.

  1. Matrix505

    Matrix505

    Joined:
    Jun 23, 2017
    Posts:
    8
    I wrote a script for my car wheels. I try to get Y rotation but if X rotation greater than 90, the Y axis euler angle returns 10 more than I expect.

    My code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MovementTier : MonoBehaviour
    {
    public float speed;

    public float yAngle;

    public GameObject wheel1;
    public GameObject wheel2;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    yAngle = Vector3.Angle(wheel1.transform.right, Vector3.right);

    Debug.LogWarning(yAngle);

    if (Input.GetKey(KeyCode.LeftArrow) && yAngle < 120f)
    {
    wheel1.transform.Rotate(0, -speed * Time.deltaTime, 0);
    wheel2.transform.Rotate(0, -speed * Time.deltaTime, 0);
    }
    if (Input.GetKey(KeyCode.RightArrow) && yAngle > 50f)
    {
    wheel1.transform.Rotate(0, speed * Time.deltaTime, 0);
    wheel2.transform.Rotate(0, speed * Time.deltaTime, 0);
    }

    //if (!Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.LeftArrow) && yAngle != 90) returnToCenter();
    }

    void returnToCenter()
    {
    Debug.Log("hi");
    if (yAngle < 90)
    {
    if (Mathf.Abs(90 - yAngle) <= 3)
    {
    wheel1.transform.localRotation = Quaternion.Euler(wheel1.transform.localRotation.x, 0, wheel1.transform.localRotation.z);
    wheel2.transform.localRotation = Quaternion.Euler(wheel2.transform.localRotation.x, 0, wheel2.transform.localRotation.z);
    }
    else
    {
    wheel1.transform.Rotate(0, -speed * Time.deltaTime, 0);
    wheel2.transform.Rotate(0, -speed * Time.deltaTime, 0);
    }
    }
    else if (yAngle > 90)
    {
    if (Mathf.Abs(90 - yAngle) <= 3)
    {
    wheel1.transform.localRotation = Quaternion.Euler(wheel1.transform.localRotation.x, 0, wheel1.transform.localRotation.z);
    wheel2.transform.localRotation = Quaternion.Euler(wheel2.transform.localRotation.x, 0, wheel2.transform.localRotation.z);
    }
    else
    {
    wheel1.transform.Rotate(0, speed * Time.deltaTime, 0);
    wheel2.transform.Rotate(0, speed * Time.deltaTime, 0);
    }
    }
    }
    }



    Sorry for my grammar mistakes.
     
    Last edited: Jan 6, 2020
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Yep, this is why Euler angles are terrible. For any given rotation, there are multiple valid Euler angle representations, and the three components of the Euler angle cannot be handled independently. This is a fundamental fact of the math behind Euler angles.

    Further reading and examples about avoiding problems that arise from this can be found in this article. The short answer is: don't ever read out the Euler angle value and expect to rely on it.
     
    angrypenguin and Joe-Censored like this.
  3. Matrix505

    Matrix505

    Joined:
    Jun 23, 2017
    Posts:
    8
    So... What I can do?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    The article linked contains a lot of examples, and odds are good that one of those will work in your situation. Probably checking with one of the transform.forward or transform.right vectors would be helpful.
     
    angrypenguin likes this.
  5. Matrix505

    Matrix505

    Joined:
    Jun 23, 2017
    Posts:
    8
    Thanks, I will look at that.
     
  6. Matrix505

    Matrix505

    Joined:
    Jun 23, 2017
    Posts:
    8
    I tried that, it works well with right vectors but I think it isn't best way to do that.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    What's your code, and what makes you think it isn't the best way to do that?
     
    angrypenguin likes this.
  8. Matrix505

    Matrix505

    Joined:
    Jun 23, 2017
    Posts:
    8
    In Unity documents, euler angles is recomended way to do that, am I wrong?
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    I don't know whether the Unity documents say that, but if they do, they're wrong. Euler angles are ideal solution for only one thing, which is letting a human type in rotation values (which is why they're used in the Transform Inspector). For literally everything besides that, you want to use a different solution, or you'll have problems like the one you're seeing.
     
    angrypenguin likes this.
  10. Matrix505

    Matrix505

    Joined:
    Jun 23, 2017
    Posts:
    8
    I noticed when I change rotation of parent GameObject, the solution works wrong.
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    In that case you probably should be using .localRotation in place of .rotation.

    It's hard to help without seeing the code though
     
    angrypenguin likes this.
  12. Matrix505

    Matrix505

    Joined:
    Jun 23, 2017
    Posts:
    8
    I edited code.
     
  13. Matrix505

    Matrix505

    Joined:
    Jun 23, 2017
    Posts:
    8
    Sorry, I just missed a point and my problem is solved with Euler Angles.