Vector2 Array!

Hey guys!

I am currently creating a Vector2 array to store the starting rotation of a bunch of cameras that I have in my scene.

I am getting some strange behavior and was wondering if I overlooked some fundamental.

for(var i : int = 0; i < camerasOriginalRot.length; i++)
   {
       camerasOriginalRot[i] = new Vector2(cameras[i].transform.localEulerAngles.y, cameras[i].transform.localEulerAngles.x);
       
       print(camerasOriginalRot[i]);
   }

The issue that I am having is for some reason if the value that I grab is negative, it adds 360 to the value which completely defeats the purpose of grabbing the starting rotation. I need the negatives however and I’m not sure what would be causing this. Also oddly enough, if I grab the starting rotation from the cameras array, it shows the true values with negatives and positives, but when I try adding them all prior to any player interaction, the negatives are gone. :frowning:

Thanks for any and all input guys!! :smile:

did you try .transform.rotation and transform.localrotation
there is also Quaternion.Euler() to look into.

Why are you storing rotation as only 2 of its components?

If it were 2d, you only need 1 rotation value.

If it’s 3d, you can store either all 3 of the euler values, or all 4 of the quaternion valuse (I’d go with quaternion).

What are you attempting to store here?

I just tested it anyway even with the incorrect output… and the objects that I move reset their rotation once I click on them and work totally fine… even the negative value rotations are working although what is stored in the array does not show a negative value… strange.

I think I am still going to totally change how this is being handled! I only store the X,Y because the Z is locked and does not move!

How are you locking the z axis?

Because even if it’s locked… in 3-space, multiple different euler rotations can equal the same orientation.

Take for instance:
euler(90,40,0)

There’s no z rotation here, thing is… this value:
euler(90,0,-40)

Is the SAME orientation. And funny enough if you do something like this in code:

public class zTest01 : MonoBehaviour
{

    private void Start()
    {
        transform.eulerAngles = new Vector3(90f, 40f, 0f);
    }

}

You can look in the inspector, and I bet you it’ll show (90,0,-40) despite you setting the rotation to (90,40,0).

(I’m not 100% certain of this, it does it on my machine… if you run osx though, it ‘might’ be different? No idea if unity changes stuff from platform to platform).

Basically, because whenever you set the eulerAngles property of a Transform, all it’s really doing is converting your euler angles into a Quaternion. Then when you re-read them out, it’ll calculates the euler angles from the quat.

There’s no guarantee that the value you set it with will be the same value that is returned later. So just because you only change the x and y, doesn’t mean you can just store the x and y.

That’s not how 3d rotation works.

1 Like