Rendering sprite infront of the character (tallgras)


in a 2D game i’am trying to get a effect like it is in secret of mana/seiken densetsu 2 where the tall gras gets rendered infront of the bottom half of the character when the player stands in it.


I have 2 non-optimal solutions for it.

1. The character has a box collider which triggers the gras sorting layer to switch to foreground when the player collider hits the tall gras collider. Problem: The gras doesn’t always fit exactly the bottom half of the player.

2. I have a second camera in the scene which is a child of the character and renders only the tall gras. The viewport rect of that cameras gets changed via script to match the bottom half of the character.
Problem: I’am getting ugly flickering pixels when the gras gets rendered. I think its because i calculate the viewport rect with float values relative to the character position and after rendering its snaps to the pixel units.

Code for the second solution:

     void Update ()
            //i think this is the same as that on the bottom but only in viewport units
            //Vector3 screenPos = mainCamera.WorldToViewportPoint(character.transform.position);
            //float height = 1/(mainCamera.orthographicSize*2);
            //float width = 1/(mainCamera.orthographicSize*2*mainCamera.aspect);
            //grasCamera.rect = new Rect(screenPos.x-width/2, screenPos.y-height, width,height);
        	Vector3 screenPos = mainCamera.WorldToScreenPoint(character.transform.position);
        	float height = Screen.height/(mainCamera.orthographicSize*2);
        	float width = Screen.width/(mainCamera.orthographicSize*2*mainCamera.aspect);
        	grasCamera.pixelRect = new Rect(screenPos.x-width/2, screenPos.y-height, width,height);

Does someone has another even better solution or is there already a existing solution outside?

Perform an Physics2D.OverlapCircle at the bottom of the player character and whichever grass comes inside it render it on top of the player character. This is similar to your first solution but instead of using a Trigger you can use OverlapCircle.