Search Unity

[Unity 5.5] Tutorial: How to create a Mirror

Discussion in 'Community Learning & Teaching' started by gamadgames, Mar 27, 2017.

  1. gamadgames

    gamadgames

    Joined:
    May 31, 2014
    Posts:
    141


    Hello guys!
    Today we are going to create a Mirror in unity 5!

    (This tutorial is mainly ment for beginners, so that's why I exaclty explain every detail what I do ;) )

    I hope you guys like it!

    Also if you got any questions, just ask in the comments!
     
  2. waqas_90

    waqas_90

    Joined:
    Dec 5, 2016
    Posts:
    2
    Thanks for this awesome tutorial. one question, do you know how we can reflect the light on mirror, light should point to opposite direction after interacting with mirror ! thanks again :)
     
  3. Deleted User

    Deleted User

    Guest

    Looks terrific, thank you! :D

    How is it that this thread didn't get more comments ? Also, I just went on the you tube page for this video and three people downrated it??? :(
     
    Last edited by a moderator: Oct 20, 2018
  4. Deleted User

    Deleted User

    Guest

    I've made this tutorial with Unity 2018.2 and it works rather well. I just have a slight problem. Here is your script:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Mirror : MonoBehaviour
    4. {
    5.     public Transform mirrorCam;
    6.     public Transform playerCam;
    7.  
    8.     private void Update()
    9.     {
    10.         CalculateRotation();
    11.     }
    12.  
    13.     public void CalculateRotation ()
    14.     {
    15.         Vector3 dir = (playerCam.position - transform.position).normalized;
    16.         Quaternion rot = Quaternion.LookRotation(dir);
    17.  
    18.         rot.eulerAngles = transform.eulerAngles - rot.eulerAngles;
    19.  
    20.         mirrorCam.localRotation = rot;
    21.     }
    22. }
    If I use it as it is, the mirrorCam rotates on the x axis instead of staying at 0:

    mirror1.JPG

    Things get better if I change
    Code (CSharp):
    1. rot.eulerAngles = transform.eulerAngles - rot.eulerAngles;
    in CalculateRotation(), into
    Code (CSharp):
    1. rot.eulerAngles = (transform.eulerAngles - rot.eulerAngles).normalized;
    but there is still a slight rotation (1 degree sharp) on the x axis:

    mirror2.JPG
     
  5. CenturyofSalt

    CenturyofSalt

    Joined:
    Apr 19, 2019
    Posts:
    3
    Hello,
    First, I'd like to thank you for the tutorial! It was fantastic! However, I ran into an issue with the angling of the camera and after several hours of toying around with it and trying to make the mirror work properly, I needed to take a break. The picture below is the view that I get from the mirror upon approaching it. I am very new to Unity, so I'm sure it's a simple fix, I just don't know exactly what the issue is. The camera is facing into the room, not into the sink, and yet the mirror's reflection simply wont face the right way. Even the rotation is fine, it seems to follow me as I walk around, but it just wont reflect at the right angle. Some help would be very much appreciated.

    EDIT: Changing the angle of the camera and scale of the mirror texture doesn't seem to fix the problem either. In fact, it just ends up reflecting the room that is on the other side of the wall.
     

    Attached Files:

    Last edited: Apr 19, 2019