Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

[Bug] Translating GOs is broken

Discussion in '2D Experimental Preview' started by der_r, Jun 4, 2016.

  1. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    I haven't figured out what exactly is going on yet, but there seems to be a bug with sprite flipping and translating transforms.

    The first part of the GIF is a prototype running in Unity 5.3.4. Everything works as expected. The second part where the sprite moves all about is the same project running in the preview build.



    I'm not using Rigidbody2D. The character controller uses raycasting and transform.Translate to move the sprite.
     
  2. Johaness_Reuben

    Johaness_Reuben

    Unity Technologies

    Joined:
    Jan 27, 2016
    Posts:
    253
    Did you open the project or did you recreate it? As in, is the level made with new Tile Map?
    Currently project compatibility between versions is not supported. Which is one of the reasons for the Experimental Preview, so focus can be on the features itself.
    You'll have to try importing the different parts from your other projects that you want to use.
     
  3. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    Both. I encountered the bug first in a completely fresh project that I built from scratch. After I couldn't figure out what is going on, I tried running an existing project with the preview build to confirm it and the same bug appeared using different scripts and assets.

    [Edit]

    Here is the script for reference:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpriteFlipper : MonoBehaviour
    5. {
    6.     public bool facingRight = true;
    7.     Vector3 savedPosition;
    8.     BoxCollider2D box;
    9.  
    10.     void Awake()
    11.     {
    12.         box = GetComponent<BoxCollider2D>();
    13.     }
    14.  
    15.     void Start()
    16.     {
    17.         savedPosition = transform.position;
    18.     }
    19.  
    20.     void LateUpdate()
    21.     {
    22.         savedPosition = transform.position;
    23.     }
    24.  
    25.     void OnWillRenderObject()
    26.     {
    27.         savedPosition = transform.position;
    28.  
    29.         Vector2 perfectPosition = transform.position;
    30.         perfectPosition.x = Mathf.RoundToInt(perfectPosition.x);
    31.         perfectPosition.y = Mathf.RoundToInt(perfectPosition.y);
    32.         //perfectPosition.x = Mathf.FloorToInt(perfectPosition.x);
    33.         //perfectPosition.y = Mathf.FloorToInt(perfectPosition.y);
    34.         transform.position = perfectPosition;
    35.     }
    36.  
    37.     void OnRenderObject()
    38.     {
    39.         transform.position = savedPosition;
    40.     }
    41.  
    42.     public void SetDirection(float x)
    43.     {
    44.         if (x > 0 && !facingRight)
    45.             Flip();
    46.         else if (x < 0 && facingRight)
    47.             Flip();
    48.     }
    49.  
    50.     public void Flip()
    51.     {
    52.         Vector3 oldCenter = box.bounds.center;
    53.  
    54.         // Do the flip
    55.         Vector3 theScale = transform.localScale;
    56.         theScale.x *= -1;
    57.         transform.localScale = theScale;
    58.         facingRight = !facingRight;
    59.  
    60.         // Adjust position
    61.         Vector3 newCenter = box.bounds.center;
    62.         transform.Translate(oldCenter - newCenter);
    63.     }
    64. }
    65.  
    I'm adjusting the sprite position after flipping because the GO pivot is not the middle of the bounding box. I have to have it like this to avoid sprite jitters when rendering through a pixel perfect camera.

    I currently have a workaround where the sprite is a child GO to the actual logic GO but that is something I wanted to avoid having to do for all the objects in my game.
     
    Last edited: Jun 6, 2016