Search Unity

Gaps between connected sprites

Discussion in 'Getting Started' started by AdrianHil, Feb 12, 2019.

  1. AdrianHil

    AdrianHil

    Joined:
    Feb 1, 2018
    Posts:
    26
    Hi all,
    I'm in the middle of unity 2d course and I encounter one annoying thing and I'm out of ideas how to solve this issue. The issue is like below (these sprites were joined using 'v' key):
    flickering_issue.gif

    So at first I started looking for some similar problems on the internet and I tried few things:
    - disabling anti aliasing
    - sprite size to be power of 2
    - adding thiny border to sprites to avoid situation when Unity will use transparent color for normalization
    None of this helped. I digged a bit further and I finaly found out what's the issue. I have two scripts, one is for camera to following the character and second one is for making parallax effect.
    Following character:
    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         Vector3 newCameraPosition = new Vector3(Character.transform.position.x, transform.position.y, transform.position.z);
    4.  
    5.         transform.position = Vector3.SmoothDamp(transform.position, newCameraPosition, ref currentVelocity, Smooth);
    6.     }
    Parallax effect (this script is attached to each desired sprite separately) :
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         previousCameraPosition = CameraTransform.position;
    4.  
    5.     }
    6.  
    7.     void Update()
    8.     {
    9.         deltaCameraPosition = CameraTransform.position - previousCameraPosition;
    10.         Vector3 parallaxPosition = new Vector3(transform.position.x + (deltaCameraPosition.x) * ParallaxFactor, transform.position.y, transform.position.z);
    11.  
    12.         transform.position = parallaxPosition;
    13.         previousCameraPosition = CameraTransform.position;
    14.     }
    Let me take two connected sprites as an example, one has position x= 16 and second one has x=30 (the difference is 14), when I'm starting moving my character position of these background sprites is changing (because of parallax effect) what is expected but distance between two joined sprites changing as well - so after a while position of mentioned sprites is like follow x=377.6427 and second one x=391.6438 - the difference now is 14.0011 and that's causing this ugly gaps.
    flickering_issue.gif
    I know that floating inaccuracy is well known problem but I don't have idea how could I handle that in such case.

    Could someone please give me a hand with this ?
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,183
    My solution, which is likely not the best solution, is to just periodically remove the inaccuracy. I haven't tested out the below code snippet, but it should zero out digits to the right of the decimal point when it's almost at a value that has nothing to the right of it.

    Code (csharp):
    1. if (Mathf.Approximately(value, (int)value)
    2.     value = (int)value;
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Just set the position of all the sprites in a single script, instead of individually. That way they all have identical floating point precision errors, and you won't see the gaps.
     
  4. AdrianHil

    AdrianHil

    Joined:
    Feb 1, 2018
    Posts:
    26
    Guys, thanks for your answers!

    @Ryiah I didn't check that particular solution but I did try something similar I guess:
    Code (CSharp):
    1. sprite(n+1).position.x = sprite(n).position.x + (correctDistanceBetweenTwoSprites * numberInARow);
    2.  
    3. // example
    4. // sprite(n).position.x = 10 ; difference = 5;
    5. // position for sprite(n+1) = 10 + 5 * 1 = 15;
    6. // position for sprite(n+2) = 10 + 5 * 2 = 20; etc...
    But it didn't worked unfortunately. Besides of that periodically removing the inaccuracy might leave still some issues between these periods if you know what I mean. Thanks for your idea anyway!

    @Kiwasi thanks for that tip! It's unbelievable but I was trying this solution and that was not working, after your comment I tried again and it works like a charm ha, great thanks!

    So, instead of assigning script to each sprite individually I placed all game objects under one parent object and assigned script to him.
    Before:
    upload_2019-2-13_13-20-58.png

    After:
    upload_2019-2-13_13-21-40.png
     
    Ryiah likes this.