Quaternion returning wrong value.

First time posting here asking for help. The thing is, I already resolved my issue by using another code.
But here is the new issue, I don’t understand why my code didn’t work - and it’ll be an issue later on if I’m missusing this as is.

Scenario:
I’m making a little minigame. Several rotating cylinders with an opening in them. Every cylinder moves in a different direction, and at different speeds. I’m very happy. But then I realize my code for making their starting rotation randomized doesn’t work…
This is what I had:

    [SerializeField] private Quaternion initialRotation;
    [SerializeField] private Quaternion randomStartPos;
    [SerializeField] private bool spinningRight;
    [SerializeField] private int spinningDir;
    [SerializeField] private float spinningSpeed;
   
private void Start()
    {
       //Setting and saving a random Quaternion
        randomStartPos = Random.rotation;
       
       //logic to handle if it's going left or right, and at what speed.

        spinningRight = (Random.value > 0.5f);
        if(!spinningRight)
        {
            spinningDir = -1;
        }
        else
        {
            spinningDir = 1;
        }

        spinningSpeed = spinningDir * Random.Range(0.3f, 1.0f);
       
        //This is where the issue is. It won't let me use the information from the quaternion. It just sets Z to 1.

        transform.rotation = new Quaternion(0,0,randomStartPos.z,0);

        initialRotation = transform.rotation;
    }

   

    private void Update()
    {
        if(isSpinning)
        Spin();
 

    }

    private void Spin()
    {
        {
          transform.Rotate(new Vector3(0f, 0f, spinningSpeed));
        }
    }

I ended up using:

transform.rotation = Quaternion.Euler(0,0,Random.Range(0,360));

Mainly posting it for anyone who googles this specific thing. But for myself, I’m asking this because I wanna know what I did wrong.

Thank you. :slight_smile:

The .x, .y, .z and .w fields of a Quaternion are NOT ANGLES.

Do not molest them unless you have a math degree. They are Quaternion internals.

Perhaps you want the .eulerAngles.z property?

Whatever it is, keep this in mind as you proceed:

https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

2 Likes

Thanks! This answered exactly what I needed.
Now I don’t have to worry about making that mistake, and I know why! :slight_smile: