What can I do to solve problems of z-fighting with sprites in a 3d world

So I’ve been looking for a solution to a problem i’m having creating a character with the 2d sprites in a 3d world. I’ve looked at a couple of similar questions but they havent given me the full solution to the problem. Btw even with an orthographic camera it gives me the problem and I also tried changing the near and far clips of the camera with no success.

This post is the one that helped me the most but its giving me a new problem:

So my character is composed of a couple of quads with the sprite renderer component to create a player that moves. All of the quads are in a different Z and I can’t use the sorting layer that unity 2d provides since these characters are going to be moving back and forward in the z plane.
Here is an image with the problem:

24848-image.jpg

Original: The original is when i look at it straight, as you can see it doesn’t give me any problems, the problem is when I angle the camera (which i need to for my game)

Original Angled: As you can see once i angle the camera the Z-fighting starts as it just goes and cuts off his hand :frowning:

Custom Shader: I created the custom shader exactly as the post above said and it did fix most of the problems when i angled the camera but i gave me new ones, they are the ones highlighted in the red boxes, they appear to be caused by the alpha from the sprites which seem to cut out the stuff that is close behind it and they start showing up more and more when you angle the camera, for example look at the two images of the head, one shows the neck and once i tilt the camera a bit more it cuts it right off.

I don’t really know much about shaders, I’ve been looking around and I haven’t been able to find and answer, what should I do?

Just in case here is the shader that i’m using:

Shader "Sprites/DefaultZ"
{
	Properties
	{
		[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
		_Color ("Tint", Color) = (1,1,1,1)
		[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
	}

	SubShader
	{
		Tags
		{ 
			"Queue"="Transparent" 
			"IgnoreProjector"="True" 
			"RenderType"="TransparentCutout" 
			"PreviewType"="Plane"
			"CanUseSpriteAtlas"="True"
		}
		
		Cull Off
		Lighting Off
		ZWrite On
		Fog { Mode Off }
		Blend SrcAlpha OneMinusSrcAlpha
		Pass
		{
		
		CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile DUMMY PIXELSNAP_ON
			#include "UnityCG.cginc"
			
			struct appdata_t
			{
				float4 vertex   : POSITION;
				float4 color    : COLOR;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f
			{
				float4 vertex   : SV_POSITION;
				fixed4 color    : COLOR;
				half2 texcoord  : TEXCOORD0;
			};
			
			fixed4 _Color;

			v2f vert(appdata_t IN)
			{
				v2f OUT;
				OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
				OUT.texcoord = IN.texcoord;
				OUT.color = IN.color * _Color;
				#ifdef PIXELSNAP_ON
				OUT.vertex = UnityPixelSnap (OUT.vertex);
				#endif

				return OUT;
			}

			sampler2D _MainTex;

			fixed4 frag(v2f IN) : COLOR
			{
				return tex2D(_MainTex, IN.texcoord) * IN.color;
			}
		ENDCG
		}
	}
}

You should probably use Sorting Layer and Order in Layer. That would be much easier than messing with Shaders and it gives you full control over rendering order.

These properties are documented in Sprite Render, and there’s also a tutorial available: Tutorials / Topics / 2D / Sorting Layers

Yeah, those usually do the trick, problem is that the game has some 3d to it, so if I assign a sorting layer and another character is behind him if would look weird due to the sorting layer, it would show a piece of the arm of the player who is behind the other one. But thanks for the comment :slight_smile: