How to dynamically hide part of 2D sprite?

I’d like to put some transparency to my 2D sprite during runtime. In my game I cut the tree down by moving its box collider 2D so that the user see it’s shrinking but the problem is that I can still see the bottom part of the tree under the ground. Is there any way to hide sprite’s texture e.g. when it’s under X axis? I cannot use ‘order in layer’ beacuse of couple of reasons.

Shader “randomTest” {
Properties {
_MainTex (“Base (RGB)”, 2D) = “white” {}
}
SubShader {
Lighting Off
AlphaTest Greater 0.5

		Tags
		{ 
			"Queue"="Transparent" 
			"IgnoreProjector"="True" 
			"RenderType"="Transparent" 
			"PreviewType"="Plane"
			"CanUseSpriteAtlas"="True"
		}
		
		Cull Off
		Lighting Off
		ZWrite Off
		Fog { Mode Off }
		Blend One OneMinusSrcAlpha
		LOD 200
		
    CGPROGRAM
	#pragma surface surf NoLighting
    #include "UnityCG.cginc"
	
    fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten){
		fixed4 c;
		c.rgb = s.Albedo;
		c.a = s.Alpha;
		return c;
    }
	
    sampler2D _MainTex;
	
    struct Input {
		float2 uv_MainTex;
		float3 worldPos;
    };
	
    void surf (Input IN, inout SurfaceOutput o) {
		clip ((IN.worldPos.y /*Adding a value where this text is will change at what height the sprite will not be rendered*/));
		half4 c = tex2D (_MainTex, IN.uv_MainTex);
		o.Albedo = c.rgb;
		o.Alpha = c.a;
    }
    ENDCG
   }
    FallBack "Diffuse"
}

Make a material with this shader and add it to the sprite. This shader should work. With it all you have to do is transform the tree along chosen axis. Parts of the sprite under x axis will not be rendered. This might change the look of the sprite a slight bit, I suggest for looking more into shaders. Good luck!

@PhoenixBlackReal thank you for your anwer it was very helpful.

I am adding a parameterized solution of the shader u posted that happened to be needed for my case in case someone else can find a use for this.

 Shader "Custom/PositionBasedTransparency" {
    Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_LeftLimit("Left Limit: World Pos X", Float) = -2.0
		_RightLimit("Right Limit: World Pos X", Float) = 2.0
		_TopLimit("Top Limit: World Pos Y", Float) = -2.0
		_BottomLimit("Bottom Limit: World Pos Y", Float) = 2.0
	}
	SubShader {
		Lighting Off
		AlphaTest Greater 0.5
             
        Tags { 
             "Queue"="Transparent" 
             "IgnoreProjector"="True" 
             "RenderType"="Transparent" 
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
        }
         
		Cull Off
        Lighting Off
        ZWrite Off
        Fog { Mode Off }
        Blend One OneMinusSrcAlpha
        LOD 200
         
		CGPROGRAM
		#pragma surface surf NoLighting
		#include "UnityCG.cginc"
     
		fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten){
			fixed4 c;
			c.rgb = s.Albedo;
			c.a = s.Alpha;
			return c;
		}
     
		sampler2D _MainTex;
		float _LeftLimit;
		float _RightLimit;
		float _TopLimit;
		float _BottomLimit;

		struct Input {
			float2 uv_MainTex;
			float3 worldPos;
		};
     
		void surf (Input IN, inout SurfaceOutput o) {
			// clip will not display the current pixel when the argument passed has a non positive value
			if (IN.worldPos.x - _LeftLimit < 0 || _RightLimit - IN.worldPos.x < 0 ||
				IN.worldPos.y - _TopLimit < 0 || _BottomLimit - IN.worldPos.y < 0) {
				clip(-1.0);
			}

			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	}
    FallBack "Diffuse"
 }

I don’t use surface shader because surface shader will cause the sprite bright.And it can work fine in Unity5.

Shader "Sprites/Default_"
{
	Properties
	{
		[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
	_Color("Tint", Color) = (1,1,1,1)
		[MaterialToggle] PixelSnap("Pixel snap", Float) = 0
		LeftLimit("Left Limit: World Pos X", Float) = -2.0
		_RightLimit("Right Limit: World Pos X", Float) = 2.0
		_TopLimit("Top Limit: World Pos Y", Float) = -2.0
		_BottomLimit("Bottom Limit: World Pos Y", Float) = 2.0
	}

		SubShader
	{
		Tags
	{
		"Queue" = "Transparent"
		"IgnoreProjector" = "True"
		"RenderType" = "Transparent"
		"PreviewType" = "Plane"
		"CanUseSpriteAtlas" = "True"
	}

		Cull Off
		Lighting Off
		ZWrite Off
		Blend One OneMinusSrcAlpha

		Pass
	{
		CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile _ PIXELSNAP_ON
#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
#include "UnityCG.cginc"

		struct appdata_t
	{
		float4 vertex   : POSITION;
		float4 color    : COLOR;
		float2 texcoord : TEXCOORD0;
		UNITY_VERTEX_INPUT_INSTANCE_ID
	};

	struct v2f
	{
		float3 worldPos : TEXCOORD1;
		float4 vertex   : SV_POSITION;
		fixed4 color : COLOR;
		float2 texcoord  : TEXCOORD0;
		UNITY_VERTEX_OUTPUT_STEREO
	};

	fixed4 _Color;
 

	v2f vert(appdata_t IN)
	{
		v2f OUT;
 
		UNITY_SETUP_INSTANCE_ID(IN);
		UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
		OUT.vertex = UnityObjectToClipPos(IN.vertex);

		OUT.worldPos = mul(unity_ObjectToWorld, IN.vertex).xyz;

		OUT.texcoord = IN.texcoord;
		OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
		OUT.vertex = UnityPixelSnap(OUT.vertex);
#endif

		return OUT;
	}

	sampler2D _MainTex;
	sampler2D _AlphaTex;
	float _LeftLimit;
	float _RightLimit;
	float _TopLimit;
	float _BottomLimit;
	

	fixed4 SampleSpriteTexture(float2 uv)
	{
		fixed4 color = tex2D(_MainTex, uv);

#if ETC1_EXTERNAL_ALPHA
		// get the color from an external texture (usecase: Alpha support for ETC1 on android)
		color.a = tex2D(_AlphaTex, uv).r;
#endif //ETC1_EXTERNAL_ALPHA

		return color;
	}

	fixed4 frag(v2f IN) : SV_Target
	{
		 
		if (IN.worldPos.x - _LeftLimit < 0 || _RightLimit - IN.worldPos.x < 0 ||
		IN.worldPos.y - _BottomLimit < 0 || _TopLimit - IN.worldPos.y < 0) {
			clip(-1.0);
		}

		fixed4 c = SampleSpriteTexture(IN.texcoord) * IN.color;
	c.rgb *= c.a;
	return c;
	}
		ENDCG
	}
	}
}