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

Bug 2D Sprite Not Appearing Right Away?

Discussion in '2D' started by LoonyMoonyProductions, May 24, 2023.

  1. LoonyMoonyProductions

    LoonyMoonyProductions

    Joined:
    May 24, 2023
    Posts:
    2
    So, I'm making a vintage-style pixel art 2D game (think Pokemon or Harvest Moon, top-down 2D game), and I've run into an interesting bug. I've got my sprites layered to a background (your usual tiled grass), some forageable items (like certain herbs and collectibles) on a second layer, and then my Player on its own, third layer. When I go to play it, I'll walk around and my Player and Backgrounds are just fine, but sometimes the Forage sprites will blink in... late. Three or four steps after they should have appeared at the edge of the screen, they'll appear where they should have been, but I'm only just now seeing them. They blink out a step or two early before they hit the edge of the screen, too.

    The strangest thing is, not all of them do this! Some are perfectly static and appear and disappear at the edge of the screen like one would expect, but other ones sit there, invisible, and pop up randomly. This is seriously unacceptable -- imagine if that was a monster that appeared out of nowhere and attacked you! Players would get pissed. And it's not like I'm at the edge of the map, either. It's super strange.

    I'm looking online for anyone else with this problem, but it's either stuff about layering (which I'm already doing), the camera being on the wrong spot on the Z-axis (which is also not the thing -- it's there, the sprite is just showing up on a delay), or people wanting a blink animation on their sprites, which isn't what I'm looking for, either. Is this a rendering issue? A glitch with the camera, or the sprites? Is there a plug-in or bit of code I can add to make sure this doesn't happen?

    Has anyone else seen something like this before?

    Inb4, I'm super new to Unity, be gentle, ha.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Using the scene view at the same time, zoom in on one of the flickering bits.

    Is it flickering in the scene? Then figure out why. Animation? Scripting?

    If it is NOT flickering then it is most likely layering, so go back and recheck it, maybe even at runtime move it to other layers and FORCE it to work until you find what was / is wrong.

    Here's more fun reading:

    Three (3) primary ways that Unity draws / stacks / sorts / layers / overlays stuff:

    https://forum.unity.com/threads/orthographic-camera-rendering-order.1114078/#post-7167037

    In short, as far as the Standard Rendering Pipeline,

    1. The default 3D Renderers draw stuff according to Z depth - distance from camera.

    2. SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties

    3. UI Canvas Renderers draw in linear transform sequence, like a stack of papers

    If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:

    - identify what you are using
    - search online for the combination of things you are doing and how to to achieve what you want.

    There may be more than one solution to try.

    For instance, you may even use multiple co-located cameras to more-explicitly control apparent draw ordering.

    Additional reading in the official docs:

    https://docs.unity3d.com/Manual/2DSorting.html

    And SortingGroups can also be extremely helpful in certain circumstances:

    https://docs.unity3d.com/Manual/class-SortingGroup.html

    Other rendering pipelines may have other ways of layering (HDRP and URP). Go see the latest docs for details.
     
  3. LoonyMoonyProductions

    LoonyMoonyProductions

    Joined:
    May 24, 2023
    Posts:
    2
    Huh! Your note about the layering was helpful. I tried putting the layers in their own group, and now it works just fine! Maybe it just didn't like rendering all the different pieces separately, so there was a delay in appearing.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    More likely the layering was ambiguous in some way, so it's a 50/50 proposition which gets painted first, OR which one wins the z-fighting contest.

    Google up z-fighting if you're curious for more.