Unity3D mirror using camera and RenderTexture
There are several ways to create mirror in Unity3D. Some of them require more scripting than others, but I will now present an easy way using just one extra camera and RenderTexture.
In this solution you will use RenderTexture on an object and place camera behind that object. This is easy option that does not require scripting. Alternatively you can use reflective shader, but it requires some more work.
Seven small steps to create object with mirror RenderTexture:
- Create new Unity project. It is recommended to use latest LTS version (Long Term Support).I was using Unity 2020.1.3f1 version for this example.
- Create plane object for ground and Cube for the mirror object. Scale both objects to match your needs.
- Create new camera for attaching the RenderTexture. Position it inside recently created cube and adjust direction so that it is pointing out to the direction where mirror should be reflecting the image.
- Create RenderTexture. Name it as you wish. No need to modify any other settings at this point.
- Attach RenderTexture to the camera you are going to use for mirror
- Add freshly created RenderTexture to the Cube object. Adjust cube rotation, if needed. At least I had to rotate it 180 degrees over the y-axis to get picture correct. Also, you probably need to scale cube x-axis by -1 in order to match horizontal directions.
- If you are seeing a mirror image of skybox, everything has probably gone well. Now you can add some other gameobjects to the scene and see if you can get them visible in your mirror correct way.
- Edit -> Project Settings -> Player -> Color Space: Change "Gamma" to "Linear"
- Window -> Rendering -> Lighting Settings : Select "Realtime Global Illumination" and then click "Generate Lighting"