Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug setting transform.rotation = Quaternion.Euler(0,180,180) is inconsistent

Discussion in 'Editor & General Support' started by SoftwareGeezers, Aug 30, 2020.

  1. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    I have issues with disappearing sprites in a 2D space shooter that I've traced to different behaviour with assigning a rotation to point straight down. (Unity 2019.2)

    In a simple test project,
    Code (CSharp):
    1. transform.rotation = Quaternion.Euler(0,180,180)
    ...results in a rotation of Euler (180,0,0) and the sprite is rotated and visible.

    However, in my game, the same code results in a rotation of Euler(0,180,180) and my sprite is flipped and invisible.

    This affects the underlying mechanics of transform manipulation. To date, I've been assigning transform directions using
    Code (CSharp):
    1. transform.up = direction
    . I have had some issues with flashing sprites, disappearing for a frame, that I hadn't bothered to trace up till now as I thought it was related to hidden collisions, but my current work of orbiting objects around other objects has resulted in this discovery that transform.up = direction is inconsistent when pointing straight down. transform.up = Vector3.down on some objects results in a (0,0,180) rotation, while on others, a (0,180,180) rotation.

    I have been unable to create a repo case. Simple sprites in my game in test scene work correctly, with a direct facing down being drawn (and the rotation being set to Euler(180,0,0)), even with rigidbodies and colliders attached, but the same code on my objects in game is problematic. I guess it must be something to do with instantiating and parenting and who-knows-what.

    Are Unity aware of any issues? Is this a bug requiring a submission? It's not a particularly easy submission for a bug report.

    Edit: I have a workaround. I test if the quarternion rotation is down, and if so, set it as down from euler instead of assigning the rotation directly.

    Code (CSharp):
    1.     private void Start() {
    2.         bugPatchRotation = Quaternion.FromToRotation(Vector3.up, Vector3.down);
    3.     }
    4.  
    5.     private void Update() {
    6.  
    7.        targetRotation = Quaternion.FromToRotation(Vector3.up, direction);
    8.        if (targetRotation == bugPatchRotation) {
    9.            _trans.rotation = Quaternion.Euler(0, 0, 180);
    10.            print("down"+ _trans.rotation.eulerAngles);
    11.        } else {
    12.            _trans.rotation = targetRotation;
    13.        }
    14.  
    Note that assigning this
    _trans.rotation = Quaternion.Euler(0, 0, 180);
    results in transform rotation (0,0,180), but assigning this:
    _trans.rotation = Quaternion.Euler(180, 0, 0);
    results in transform rotation (0,180,180).

    That is, this transform behaves differently to other transforms in how it deals with 180 degree rotations.
     
    Last edited: Aug 30, 2020