Rotation in Unity3D

36,109

Solution 1

transform.rotation retrieves a Quaternion. Try transform.rotation.eulerAngles.y instead.

Solution 2

Transform Rotation is used for setting an angle, not turning an object, so you would need to get the rotation, add your change, and then set the new rotation.

Try using transform.rotate instead.

Check the Unity3d scripting reference here: http://unity3d.com/support/documentation/ScriptReference/Transform.Rotate.html

Solution 3

I see two problems so far. First the hierarchical rotation of Unity. Based on what you are trying to achieve you should manipulate either

transform.localEulerAngles
or
transform.eulerAngles

The second thing is, you can't modify the euler angles this way, as the Vectors are all passed by value:

transform.localEulerAngles.y--;

You have to do it this way:

Vector3 rotation = transform.localEulerAngles;
rotation.y--;
transform.localEulerAngles = rotation;
Share:
36,109
William
Author by

William

Updated on September 13, 2020

Comments

  • William
    William over 3 years

    This is a simplified code from what I'm trying to do:

    var angle = 1.57;
    if ( this.transform.rotation.y > angle ){
      this.transform.rotation.y--;
    } else if ( this.transform.rotation.y < angle ){
      this.transform.rotation.y++;
    }
    

    I'm used to code in AS3, and if I do that in flash, it works perfectly, though in Unity3D it doesn't, and I'm having a hard time figuring out why, or how could I get that effect.

    Can anybody help me? Thanks!

    edit:

    my object is a rigidbody car with 2 capsule colliders driving in a "bumpy" floor, and at some point he just loses direction precision, and I think its because of it's heirarchical rotation system.

    (thanks to kay for the transform.eulerAngles tip)