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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Hide Part of 2D Sprite

Discussion in 'Scripting' started by unbootabru, Aug 4, 2014.

  1. unbootabru

    unbootabru

    Joined:
    Jul 21, 2014
    Posts:
    6
    I am struggling to figure out how to dynamically hide portions of a sprite. In my scene, when objects cross in front of a 'mirror', an identical sprite is created so that it looks like the object is in front of a mirror. However, I am struggling to figure out how to only display the part of the sprite that is in front of the mirror. For example, when the object is only halfway into the mirror, only half of the sprite should be rendered. I have tried working with the DepthMask shader, but I am cannot get it to work. I am not exactly sure what I am supposed to do with the SetRenderQueue.cs file, I tried attaching it to the masking objects (as suggested here in the alternative uses), but it doesn't seem to mask any of my sprites.

    I have also tried using the sorting layers to hide the sprites. With sorted layers, the sprites are hidden only if I display the masking objects in the camera's culling mask, but I don't really want to be stuck with visible objects surrounding the mirror.

    Any help would be appreciated.

    This is in Unity Free btw, so RenderToTexture is out.
     
    Last edited: Aug 4, 2014
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
    Some images would be helpful.
    Does your your players back face the cam, etc?

    I would think. Tho, that using a camera, dedicated to the mirror would be helpful.
     
  3. unbootabru

    unbootabru

    Joined:
    Jul 21, 2014
    Posts:
    6
    Yeah, I initially tried using a mirror cam, but I was unable to figure out how to capture the mirror-camera with my player's vision cam. Attached is an image explaining the idea I am looking for. m.png
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
    I know you can define a cameras area in space. For instance you can define two cameras to show split screen. So, you can also define a camera to define a rectangular mirror.
     
  5. unbootabru

    unbootabru

    Joined:
    Jul 21, 2014
    Posts:
    6
    Sorry, i didn't explain myself very well. I had a mirror cam working in that captured what was in front of the mirror and displayed it on the screen; however, the mirrored image was displayed OVER the object, which is incorrect. If there is a way to layer the viewport behind GameObjects, then that would probably work.
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
    You can layer which camera goes on top of which.
     
  7. Watapax

    Watapax

    Joined:
    Sep 3, 2013
    Posts:
    34
    I think you can solve your problem only with the SortOrder Option of the Sprite and using a transparency sprite to emulate a mirror. Using just one camera. I make a draw to ilustrate the idea.
     

    Attached Files:

  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
  9. unbootabru

    unbootabru

    Joined:
    Jul 21, 2014
    Posts:
    6
    watapax, ill take a look.
    renman, that requires pro-but exactly what i wish i could use
     
  10. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    That doesn't work Watapax.