Search Unity

2D Reflections - Need Help

Discussion in '2D' started by Witfon, Dec 2, 2019.

  1. Witfon

    Witfon

    Joined:
    Nov 30, 2019
    Posts:
    2
    I'm trying to make 2D reflections in a top-down game where the camera can move vertically and horizontally, following the player. My current setup is simple: a second camera that also follows the player and only renders objects on a separate reflection layer. The reflection camera renders to a texture that has a slight vertical offset and that is then rendered by my main camera.

    My issue is that I couldn't get the reflections to flip properly.

    I tried flipping the reflection texture object but it only renders properly in one axis. The second axis gets completely flipped. I also tried tweaking the render texture in Shader Graph but without any success.

    Is there anything I can do to solve this?
    UncuccessfulReflections.png
     
    MisterSkitz likes this.
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @Witfon,

    Um, when you say you flip reflection texture, that will not work for a 2D game. If I understood correctly that you would take snapshot of something like the screen you show in your image, and then flip it upside down? Think what would happen to that square block in the water. It would end up where that square block is on the ground. On logic level you would need to have own ground level/X-axis for each object, and they would mirror to the water around that axis.

    So, you would probably need to create tiles that are under (in 2D space) the tiles that you want to reflect, and then flip them upside down. Either with code somehow or just by plain old-school upside down reflection tile.
     
  3. Witfon

    Witfon

    Joined:
    Nov 30, 2019
    Posts:
    2
    I can use that method to achieve what I need but it would take a lot of time to properly set up each individual tile type and it would take up a decent chunk of performance because of all the extra reflection objects/sprites, which is why I tried to solve this by using a method similar to screen-space reflections.

    Additionally, I'm not sure how to set this method up to work with random particles and sprites that have specific shaders that change their effect based on position in world.
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well, no need to complain to me, I know what it might or might not require to get it working :). Your own suggestion is not really possible and I in turn suggested a method I have used earlier and which works. And if you are using TileMap, I think it might possible to automate creation of reflections; you could sample the map and then check if there's empty edge that fits certain rules and then create an upside down flipped tile to another TileMap layer which would store your reflections. But that's something you need to research yourself.

    For random things you would need to take account them by creating reflection for them too. i.e. if nothing else, clone them and flip them upside down and then render them so that you can capture them to your reflection setup... Maybe they are just under a transparent water layer. Or a rendertexture composited to non-transparent surface with a shader.
     
  5. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    What are you doing to flip the player to begin with? Are you using FlipX and FlipY? I remember having a bit of trouble with FlipY with movement before so I prefer not to use it. I think accessing the rotation may be the better way to go here. You may not be able to 100% pull off what you're trying to do but you can at least get close. Give me a code example and I will see what I can do to help you.

    I also wonder if you can make your image a child of the player object and rotate it in each direction. Have it SetActive(false) and just SetActive(true) based upon which direction your player is facing.