Unity rotation issues

I'm trying to have a model rotate 90 degrees when a button is pressed - should be simple, right? Well, the entire system is a buggy mess for some odd reason. I would appreciate some help fixing it

transform.parent.rotation = Quaternion.Euler(transform.parent.rotation.x, transform.parent.rotation.y , transform.parent.rotation.z);   

Instead, the model just rotates in random directions that seem like they shouldn't at all be related to my code.

I started up the game to rotate the model while it's in play-mode, but the way it rotates seems like it just suddenly changes out of the blue.

I'm really confused by this & would appreciate some help in fixing it

1 answer

  • answered 2018-07-11 08:59 Fredrik Widerberg

    You code doesnt work like your think.

    Quaterinion.Euler expects input in the form of Euler angles, but you are inputing the (x,y,z) of a Quaterinon which consists of (x,y,z,w) which is why you get really funky rotation.

    https://docs.unity3d.com/ScriptReference/Quaternion.html

    To get the current Euler Angles of your transform, simply use transform.eulerAngles (or in your case, transform.parent.eulerAngles)

    var euler = transform.parent.eulerAngles;
    transform.parent.rotation = Quaternion.Euler(euler.x, euler.y, euler.z);
    

    However this doesnt change the rotation in anyway. If you want to rotate 90 degrees around the Y-axis, you could add 90 like this

    var euler = transform.parent.eulerAngles;
    transform.parent.rotation = Quaternion.Euler(euler.x, euler.y+90, euler.z);
    

    An even simpler way to rotate 90 degrees around Y is ofcourse

    transform.Rotate(0, 90, 0);