Search Unity

Sometimes getting a gap on side-by-side GameObjects when moving them

Discussion in '2D' started by Zwyk, Mar 26, 2015.

  1. Zwyk

    Zwyk

    Joined:
    Feb 15, 2015
    Posts:
    5
    Hello,

    I am making a scene in my game where the background is moving (according to a parallax scrolling).

    My problem is that sometimes 2 side-by-side GameObjects show a gap between the 2 GameObjects (their Sprites) that lasts 1 frame and occurs somehow randomly every 1 to 10 seconds.

    To simplify the problem and eliminate any other thing that might have interfere, I made a scene with just some GameObjects with the background's Sprite each at 19.2 units from eachother (which is also their size).

    Here's a screenshot of the problem : Capture d'écran 2015-03-26 17.19.15.png

    Basically the script just moves each GameObject with the background sprite by deltaTime and the speed :
    Code (CSharp):
    1. position.x += Time.deltaTime * speed
    I also tried using the same deltaTime for each object but it didn't change much.

    At the moment of the screenshot, the difference of the 2 GameObjects is 19.2001 units, so 0.001 unit too much. It isn't even the most I'm getting after some tests and it doesn't even seem to be a minimum causing the gap (sometimes more than 0.001 doesn't show a gap).

    At first I tried to always set the position at a 0.01 (1 pixel) precision but all it did was having larger gaps, although it was probably less common.

    I also tried to move the objects according to their distance to a moving reference, but even if it (maybe) gets me less common gaps, it still happens and it is really inconvenient.

    I'm struggling to know what I could do, I hope some of you will have an idea of what I could do or do wrong.
     
  2. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    What happens if you make a parent game object and place both of these objects beneath the parent as children then only move the parent object?
     
    theANMATOR2b likes this.
  3. Zwyk

    Zwyk

    Joined:
    Feb 15, 2015
    Posts:
    5
    Oh that's a really good idea I don't even know why I didn't think about that ! It's working like a charm :)

    I guess that if you want some objects to be linked in their position you have to parent them...
     
    Last edited: Mar 26, 2015
  4. Zwyk

    Zwyk

    Joined:
    Feb 15, 2015
    Posts:
    5
    Hello, I'm coming back to you because the problem is still here.

    I thought moving the parent of the objets had solved the problem because they are now always at the exact same distance one to another, but the visual gap is still showing...

    Something to take in account is that the gap showed depends on the point of view. In the Scene tab I might not see a gap showed on the Game tab without zooming in or out while I could also see it on the Scene while it's not showed in the Game.

    May it be because of the float precision on the positions of the objets (the parent now) and the Camera ?
    EDIT : Even with a 0.01 precision (on both the Camera and the parent), once again it's still showing a gap..

    I could fill the gap on the sprite itself so it wouldn't be empty but just a bit ugly for a frame, but I really wouldn't like being forced to do this.. I hope someone has an idea !
     
    Last edited: Apr 7, 2015
  5. Leo-Yaik

    Leo-Yaik

    Unity Technologies

    Joined:
    Aug 13, 2014
    Posts:
    436
    Hi

    Can you try the following

    1. Create a new material and select the Sprites/Default shader
    2. Check the "Pixel Snap" check box from the shader
    3. Assign the material to your Sprite Renderer

    Does the gap still shows?
     
  6. Zwyk

    Zwyk

    Joined:
    Feb 15, 2015
    Posts:
    5
    Hello,

    Yes I'm still having the gaps, even with the Pixel Snap activated.

    I changed a bit my simulation scene of the problem by making the Camera move (as in my real scene) and I've no idea why but now I'm also getting a gap on the "ground" sprite, which isn't moving at all ! And I'm not getting the gap on the ground on my real scene as far as I know (well, fortunaly we could say).

    I can't tell if that's really reliable but I feel like the gap shows more and more often as the Scene plays, or at least I'm getting it more often at some positions every time. Between ~150 and 200 (so 2 or 3 minutes in) for example it may happen once or twice per second, while it almost didn't happen or just a few times since the beginning.

    Getting a screenshot of a frame where there is a gap is a real pain, but here I just got couples or gaps just in the second. Just to show the 2 showed hills always are sticking together on their localPositions :
    Capture d'écran 2015-04-09 14.25.44.png
    Capture d'écran 2015-04-09 14.25.46.png

    Here I'm using a test spritesheet with just the 2 sprites. I'm also using the Packing Tag without which the sprites don't "stick" together at all (may it be linked ?).
    Capture d'écran 2015-04-09 14.35.02.png

    I'll post exactly what I have at the moment if that can help, but on my test scene I'm really not doing much :
    Code (CSharp):
    1. void Update()
    2. {
    3.     Vector3 pos;
    4.  
    5.     pos = Camera.main.transform.localPosition;
    6.     pos.x += Time.deltaTime;
    7.     Camera.main.transform.localPosition = pos;
    8.  
    9.     pos = hills.transform.localPosition;
    10.     pos.x = Camera.main.transform.localPosition.x * hills.GetComponent<Scrolling>().RelativePercent;
    11.     hills.transform.localPosition = pos;
    12. }
    "hills" being the manager/parent of the hills gameObjects / sprites.

    I'm also using a script I made for each Hills and Ground Manager that adds the relative background children as the camera scrolls. Basically here it just checks if the current background is leaving the camera's field of view and adds the next one right next to the other. Now that they are children of the managers (and not moving by themselves as I did when I originally posted), I'm sure that those are at a constant distance from each other so I don't think this script might be the problem (which is why I'm using it in my test scene now).

    The 2 GameObjects parenting the Managers and the Camera aren't doing anything except the first one is having a *4 scale.
    On my real scene I don't have the Camera as a child of those, I just wanted to test if it would change anything.
     
    Last edited: Apr 9, 2015
  7. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    You will see the gaps in the Editor because the resolution is different from the game. Or I guess it is at least.

    To get it pixel perfect you need to clamp your GOs to pixels. Meaning they should all positioned at pixel increments not positioned at 1/3 or 1/2 pixels and so forth. You will need to do this in for yourself in code because the pixel snap seems to do nothing. Then make sure your camera size is set to 1/2 of the height of your screen. If you set the camera size to 1/2 the height of the Editor game window and have the pixel clamping implemented the gaps should go away.

    That is off the top of my head. At work on cell phone now. But try these things. If you still have gaps I can revisit this tonight.
     
  8. Zwyk

    Zwyk

    Joined:
    Feb 15, 2015
    Posts:
    5
    Well the gaps are showing in both the Editor and the Game depending on the zoom but I guess the resolutions might interfere aswell. However I don't think it's the reason of the problem itself.

    As I said I already tried and I try each time I change something having the positions "pixel perfect" and it never fixed it.
    I'd also like not having to do it because the jerky effect resulting is kind of ugly here :/

    I'm not sure to understand your point with the height of the window, what do you mean set size to 1/2 the size of the screen/editor game window ? Once the scene/game built people might run it in whatever window size, and as of what I tested the size of the window doesn't matter to see the gap (not such as the zoom).
     
    Last edited: Apr 9, 2015