0

I am making a topdown tilebased 2D game. In this game, I want to make 1 of the walls into a mirror, like you can see on this video. Now, I know the game in the trailer is made in RPG Maker, but I want to make my game in Unity 3D.

I have tried to set a camera right next to the mirror, add a RenderTexture on the camera and set that texture on the Sprite, but of course it is not possible to convert a RenderTexture to a Sprite, so this did not end up working.

So my question is, is it possible to create a mirror like in the trailer?

4
  • you should ask this question at gamedev.stackexchange.com
    – neuhaus
    Commented Oct 11, 2017 at 12:09
  • "it is not possible to convert a RenderTexture to a Sprite" It is possible and you are on the right track. Please post that code and tell us where you are stuck.
    – Programmer
    Commented Oct 11, 2017 at 12:10
  • @Programmer I currently don't have any code. I figured the mirror should work like a minimap, so I decided to follow this tutorial: www.youtube.com/watch?v=3-CN2DXqJjM&t
    – FlorisdG
    Commented Oct 11, 2017 at 12:15
  • @neuhaus I have, but since that stachexchange is not that popular I got no response and I figured this might require some code so I thought this would be a fine location to ask this.
    – FlorisdG
    Commented Oct 11, 2017 at 12:21

2 Answers 2

1

It is possible to get that effect. Just parent the second camera to your character and make it move with your character.

It is possible to convert RenderTexture to a Sprite. First of all, convert the RenderTexture to Texture2D then convert the Texture2D to Sprite with the Sprite.Create function.

It is better to disable the second or mirror camera then use mirrorCam.Render() to manually render it only when you need to. The script below should get you started. Attach it to an empty GameObject then assign the mirror camera and the target SpriteRenderer from the Editor and it should mirror the what the camera is seeing to the SpriteRenderer. Don't forget to plugin RenderTexture to the mirror camera.

public class CameraToSpriteMirror: MonoBehaviour
{
    public SpriteRenderer spriteToUpdate;
    public Camera mirrorCam;

    void Start()
    {
        StartCoroutine(waitForCam());
    }

    WaitForEndOfFrame endOfFrame = new WaitForEndOfFrame();

    IEnumerator waitForCam()
    {
        //Will run forever in this while loop
        while (true)
        {
            //Wait for end of frame
            yield return endOfFrame;

            //Get camera render texture
            RenderTexture rendText = RenderTexture.active;
            RenderTexture.active = mirrorCam.targetTexture;

            //Render that camera
            mirrorCam.Render();

            //Convert to Texture2D
            Texture2D text = renderTextureToTexture2D(mirrorCam.targetTexture);

            RenderTexture.active = rendText;

            //Convert to Sprite
            Sprite sprite = texture2DToSprite(text);

            //Apply to SpriteRenderer
            spriteToUpdate.sprite = sprite;

        }
    }

    Texture2D renderTextureToTexture2D(RenderTexture rTex)
    {
        Texture2D tex = new Texture2D(rTex.width, rTex.height, TextureFormat.RGB24, false);
        tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
        tex.Apply();
        return tex;
    }

    Sprite texture2DToSprite(Texture2D text2D)
    {
        Sprite sprite = Sprite.Create(text2D, new Rect(0, 0, text2D.width, text2D.height), Vector2.zero);
        return sprite;
    }
}
1
  • 1
    This has helped me a lot! Thanks
    – FlorisdG
    Commented Oct 11, 2017 at 14:52
0

You could do it the good old Super Mario 64 way and have the wall be a screen that shows another camera's perspective of another character.

Unity is pretty good at PIP (Picture In Picture) from what I've heard, so may be worth a shot.

3
  • Yeah that's what I am trying to do
    – FlorisdG
    Commented Oct 11, 2017 at 12:47
  • Sorry about that, should have read more carefully. Actually, the way Mario worked wasn't by PIP, the room (and Mario) was just copied and flipped, so essentially you're controlling two characters, only that one is flipped. No PIP needed, just a transparent sprite for a glass effect, and that's that. No extra camera should be needed either
    – Will
    Commented Oct 11, 2017 at 13:04
  • Additionally, I found this thread: adventure-creator.wikia.com/wiki/2D_Mirror/…
    – Will
    Commented Oct 11, 2017 at 13:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.