Search Unity

2D Sprites, Perspective Camera, Z-Fighting, Render Order

Discussion in '2D' started by obscene, Dec 1, 2013.

  1. obscene

    obscene

    Joined:
    Oct 26, 2013
    Posts:
    34
    I know there are other topics on this but they are all over my head at the moment as I'm on about Day 3 of learning Unity. I choose perspective camera for the true parralax scrolling as well as the fact that I will actually use some 3D elements. Plus my experience with scripting fake parallax in GM Studio taught me it was a pain to position elements exactly where you wanted them in perspective to the rest of the scene.

    My game is a sillouette game and I really probably don't even notice all the z fighting that is occuring, but when I added in a fog sprite at z layer 1 to appear in front of my player field at z layer 0, I started seeing this problem.

    My camera is set at -10, and I set clipping as tight as I could for my game, near plane at 1, far plane at 50 with a 45 degree field of view. However I am noticing that I can set my camera much much further back, use a tight field of view and then exaggerate the z distance between objects to avoid the z fighting. I guess this works because the distance between the sprite origins are so large on the z plane and the x and y distances are so small they don't have as much effect.

    However I know that later on I will have enemies with many parts, some of which will have lights and such and stand out from the normal sillouette style, that I will need to have much tighter on the z range and I worry this problem will continue to bite me. Also I wonder what will happen when I have a very large background image and I move a long distance away on the x axis from it's center. And if I had to make adjustments to my camera further into development, I would have to adjust all my level design work accordingly. So I worry the camera trick might be a bad idea.

    So, as a total noob, without understanding shaders or any of the inner-workings of the graphics pipeline, is there something simple I can do that controls the draw order based on the z, or maybe based on a layer assignment, and how would I set up the layers?

    Any enlightenment is appreciated!
     
    Last edited: Dec 1, 2013
  2. bryantdrewjones

    bryantdrewjones

    Joined:
    Aug 29, 2011
    Posts:
    147
    Start by setting myCamera.transparencySortMode to TransparencySortMode.Orthographic. That should hopefully clear up most or all of your z-fighting issues. If you're still seeing problems, I would try setting the sorting layer on all of your game objects with renderer components (only SpriteRenderer exposes this property in the inspector, but all renderers are able to use it through script).

    I hope that helps :)
     
    corpsinheretoo likes this.
  3. obscene

    obscene

    Joined:
    Oct 26, 2013
    Posts:
    34
    That helped me all kinds....

    void Start ()
    {
    camera.transparencySortMode = TransparencySortMode.Orthographic;
    }

    and BOOM! Fixed.

    I had a little trouble with the capitalization of the second TransparencySortMode and the non capitalization of the first. I can't get my head around the differences, is there a dummies guide for this stuff anywhere???
     
  4. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    those are basic programming naming conventions which you will usually bump into when learning first steps about any language
     
  5. obscene

    obscene

    Joined:
    Oct 26, 2013
    Posts:
    34
    I took C++ in college 15 years ago, didn't do well and don't remember any of it lol. My only other experience is a little PHP and GML.
     
  6. TheCatProblem

    TheCatProblem

    Joined:
    May 26, 2011
    Posts:
    22
    The scripting overview in the Unity documentation is a good place to start. (Unfortunately the location of this section of the documentation seems to change every several months.) The script reference is also very useful.

    Regarding capitalization: in Unity, classes, functions, and static variables are usually capitalized, while member variables (which can refer to instances of classes) are not. As such, "transparencySortMode" is a member variable of "camera" (which is itself a member variable of the object to which your script is attached), but "TransparencySortMode" is a reference to the TransparencySortMode class in general. In other words, "transparencySortMode" is a specific instance of the "TransparencySortMode" class (similarly, "camera" is a specific occurrence of the "Camera" class).
     
  7. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476