Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sprites distorting when camera moves

Discussion in '2D' started by BreadWeek, Aug 12, 2014.

  1. BreadWeek

    BreadWeek

    Joined:
    Aug 12, 2014
    Posts:
    45
    I have a 2D game where the camera is moved within a deadzone around the player using the mouse. When the camera moves, every sprite distorts slightly by about a pixel. Not every pixel of the sprite moves across the screen in the same way, temporarily distorting it. It's hard to describe this effect, making it even harder to search. It's as if the sprites can't keep up with the camera, making individual sprites appear to jiggle slightly. It reminds me of screen tearing in games where vsync is disabled. The effect is more noticeable on smaller objects, where a pixel width of distortion on one side is more apparent, or on higher resolution objects even if they're scaled down to their lower res (and blurrier) counterparts.

    Most of my sprites are 1024x1024 or 2048x2048, with 100 pixels to units (default setting, I'm not sure if there's a better method), the filter mode hasn't seemed to make a difference in my problem. My orthographic camera is set to 32, though again I only did that because it seemed to fit my sprites, I don't know if there's a better standard I should be following.

    This isn't pixel art, just vector graphics made in Illustrator and saved as a png. I've found topics that sound similar to my issue but I can never be quite sure and no suggestions have worked for me. I'm completely willing to change my pixels to units/orthographic size/scale/whatever, but I'd rather not lose too much quality. Please let me know if there's any more info I can give, I don't want to continue my project until I feel like I have a grasp of how to properly manage my 2D art.

    For the record, here's my script to move the camera ("target" is the player's Transform):

    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4.    mouseXOffset = (Input.mousePosition.x / Screen.width - 0.5f) * 5;
    5.    mouseYOffset = (Input.mousePosition.y / Screen.height - 0.5f) * 5;
    6.  
    7.    mouseXOffset = Mathf.Min(5, mouseXOffset);
    8.    mouseXOffset = Mathf.Max(-5, mouseXOffset);
    9.    mouseYOffset = Mathf.Min(5, mouseYOffset);
    10.    mouseYOffset = Mathf.Max(-5, mouseYOffset);
    11.      
    12.    float x = target.position.x + mouseXOffset;
    13.    float y = target.position.y + mouseYOffset;
    14.    transform.position = new Vector3(x, y, transform.position.z);
    15. }
    16.  
    Thank you very much for any help :)
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    In your settings are you doing a vblank sync? otherwise you will get tearing.
     
  3. BeefSupreme

    BeefSupreme

    Joined:
    Aug 11, 2014
    Posts:
    279
    Try clamping your world positions to the nearest integer, then adding half a pixel (or 0.005 @ 100 units/pixel). I was getting this pixel warping as well, and that fixed it for me. I believe it's due to the way texels (origin = center) are mapped to pixels (origin = top/left). This MSDN article explains the details if you're interested: http://msdn.microsoft.com/en-us/library/windows/desktop/bb219690(v=vs.85).aspx.
     
  4. BreadWeek

    BreadWeek

    Joined:
    Aug 12, 2014
    Posts:
    45
    This method works fairly well for me when I round the camera's position to two decimal places and add 0.005, Rounding to the nearest integer makes the camera far too choppy. Is this roughly what you meant or should I be doing that with every object as well?

    Unfortunately it only seems to work when my camera's is half the screen height which puts it about 8 times closer than before but my workflow to deal with that is probably a problem for a different topic.
     
  5. BeefSupreme

    BeefSupreme

    Joined:
    Aug 11, 2014
    Posts:
    279
    I'm applying it to my world objects. My camera doesn't move though, so that may complicate things if it's not scrolling in whole pixel values. And you are right about rounding to the nearest hundredth, I was mistakenly thinking in pixels not world coords.