Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Setting for Pixel Game, Sprite seem to "move" when movement.

Discussion in '2D' started by shuskry, Oct 24, 2017.

  1. shuskry

    shuskry

    Joined:
    Oct 10, 2015
    Posts:
    474
    Hi everybody !

    I'm doing a 2D game on Android and i have a little problem with my graphics.

    When my player is moving, all pixel seem to move..
    it's not like shaking or jerk, i don't know how to explain this effect . i was thinking about my physic, but when i created a new scene with no physic and just a animation to move my player, the effect of "move" stay ...
    remove the smooth effect of my camera change nothing :/

    so i don't understand what happen...

    i don't know if on my video you can see it, but if someone can help me i'll really appreciate ^^'

    video : https://drive.google.com/open?id=0B3MWCUfXPiyNTUtodlppaG1ETDA


    Have a good night !

    PS: if someone have a good experience in 2D pixel game and want to help me, i can send my project to him if it can be usefull!
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    It's because your camera position isn't a pixel coordinate. If your camera is at a random floating point position, you're going to have partial-pixels on screen getting rounded, and that rounding is what causes that visual crawling.

    Try rounding your camera's position to the nearest pixel, or getting an asset that supports pixel-perfect cameras. You'll also need your sprites to be at pixel-perfect positions when they move if that's also desired.

    For example, here's the asset PixelPerfect with a demo of your visual artifact.
    http://pixelatto.com/upload/PixelPerfect/index.html
    https://www.assetstore.unity3d.com/en/#!/content/14498
     
  3. shuskry

    shuskry

    Joined:
    Oct 10, 2015
    Posts:
    474
    Oh thanks for you're help! i'm on the right way now ! :)

    I found a free script for pixel perfect wich work perfectly for this! but .. when i'm using it , my sprite seem to change their Z valu or order in layer :/

    My perspective in my game is down :/

    Do you know how can i fix that ?


    here the script of this free asset :

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3.  
    4. namespace GGEZ
    5. {
    6.  
    7. [
    8.     ExecuteInEditMode,                                       // Run this script in edit mode so the preview window looks good
    9.     RequireComponent (typeof(Camera)),                       // Only add this component if there is a camera
    10.     HelpURL ("http://ggez.org/posts/perfect-pixel-camera/"), // Website opened by clicking the book icon on the component
    11.     DisallowMultipleComponent,                               // Only one of these per GameObject
    12.     AddComponentMenu ("GGEZ/Camera/Perfect Pixel Camera")    // Insert into the "Add Component..." menu
    13. ]
    14. public class PerfectPixelCamera : MonoBehaviour
    15. {
    16.  
    17. // Set this value to the same value as Pixels Per Unit when importing sprites
    18. [
    19.     Tooltip ("The number of texture pixels that fit in 1.0 world units. Common values are 8, 16, 32 and 64. If you're making a tile-based game, this is your tile size."),
    20.     Range (1, 64)
    21. ]
    22. public int TexturePixelsPerWorldUnit = 16;
    23.  
    24. // Reference to the camera on this same GameObject. Found
    25. // by the OnEnable function.
    26. private Camera cameraComponent;
    27.  
    28. // Set to a value that compensates for the half-pixel offset when
    29. // rendering with Direct3D
    30. private float halfPixelOffsetIfNeededForD3D;
    31.  
    32. //---------------------------------------------------------------------------
    33. // OnEnable - Called by Unity when the component is created or enabled
    34. //---------------------------------------------------------------------------
    35. void OnEnable ()
    36.     {
    37.  
    38.     // Grab a reference to the camera
    39.     this.cameraComponent = (Camera)this.GetComponent (typeof(Camera));
    40.  
    41.     // Detect whether we are using Direct3D, because D3D rendering has a
    42.     // half-pixel offset from OpenGL rendering.
    43.     bool isD3D =
    44.             SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D9
    45.             || SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D11
    46.             || SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D12;
    47.  
    48.     // 0.4975f and not 0.5f is used because 0.5f can be represented
    49.     // as a perfect IEEE float. This means that when added to other
    50.     // floats that are imperfect, the results can sometimes be rounded
    51.     // the wrong way. It can be tricky to reproduce so this isn't part
    52.     // of the main demo.
    53.     this.halfPixelOffsetIfNeededForD3D = isD3D ? 0.4975f : 0f;
    54.  
    55.     // Run the LateUpdate immediately so that the projection gets set up
    56.     this.LateUpdate ();
    57.  
    58.     }
    59.  
    60. //---------------------------------------------------------------------------
    61. // OnDisable - Called by Unity when the component is disabled or destroyed
    62. // This function cleans up after the PerfectPixelCamera so that the
    63. // projection matrix isn't left in an altered state by this component.
    64. //---------------------------------------------------------------------------
    65. void OnDisable ()
    66.     {
    67.     if (this.cameraComponent == null)
    68.         {
    69.         return;
    70.         }
    71.     this.cameraComponent.ResetProjectionMatrix ();
    72.     this.cameraComponent = null;
    73.     }
    74.  
    75. //---------------------------------------------------------------------------
    76. // LateUpdate - Called by Unity after all other functions have run Update.
    77. // If you have other scripts that use LateUpdate, you might want to use
    78. // the Script Execution Order project setting to make this script run last.
    79. //---------------------------------------------------------------------------
    80. void LateUpdate ()
    81.     {
    82.  
    83.     // Get a local reference
    84.     Camera camera = this.cameraComponent;
    85.  
    86.     // Make sure the camera is in 2D mode
    87.     camera.transparencySortMode = TransparencySortMode.Orthographic;
    88.     camera.orthographic = true;
    89.     camera.transform.rotation = Quaternion.identity;
    90.     camera.orthographicSize = Mathf.Max (camera.orthographicSize, 0.00001f);
    91.  
    92.     // This is the code that computes the parameters needed to perfectly map
    93.     // world-space pixels to screen-space pixels.
    94.     var pixelRect = camera.pixelRect;
    95.     float texturePixelsPerWorldUnit = this.TexturePixelsPerWorldUnit;
    96.     float zoomFactor = Mathf.Max (1f, Mathf.Ceil ((1f * pixelRect.height) / (camera.orthographicSize * 2f * texturePixelsPerWorldUnit)));
    97.     float halfWidth  = (1f * pixelRect.width)  / (zoomFactor * 2f * texturePixelsPerWorldUnit);
    98.     float halfHeight = (1f * pixelRect.height) / (zoomFactor * 2f * texturePixelsPerWorldUnit);
    99.     float snapSizeWorldUnits = 1f / (zoomFactor * texturePixelsPerWorldUnit);
    100.     float halfPixelOffsetInWorldUnits = this.halfPixelOffsetIfNeededForD3D * snapSizeWorldUnits;
    101.     float pixelPerfectXOffset = halfPixelOffsetInWorldUnits - Mathf.Repeat (snapSizeWorldUnits + Mathf.Repeat (camera.transform.position.x, snapSizeWorldUnits), snapSizeWorldUnits);
    102.     float pixelPerfectYOffset = halfPixelOffsetInWorldUnits - Mathf.Repeat (snapSizeWorldUnits + Mathf.Repeat (camera.transform.position.y, snapSizeWorldUnits), snapSizeWorldUnits);
    103.  
    104.     // Build a manual projection matrix that fixes the camera!
    105.     camera.projectionMatrix = Matrix4x4.Ortho (
    106.             -halfWidth + pixelPerfectXOffset,
    107.              halfWidth + pixelPerfectXOffset,
    108.             -halfHeight + pixelPerfectYOffset,
    109.              halfHeight + pixelPerfectYOffset,
    110.             camera.nearClipPlane,
    111.             camera.farClipPlane
    112.             );
    113.     }
    114. }
    115. }
    thanks for you're help! have a good day !