Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AmasterAmaster · Jan 24, 2015 at 08:42 PM · cameramathmirror

How to turn a camera inversely from another camera (Similar to a realistic mirror)?

I am confused on this question, how can I turn the camera inversely (as in, if I look directly into a mirror, I should see a perspective facing back at me, then if I look at the mirror at a 45 degree angle, I should see a perspective reflected at a 45 degree angle outwards from the mirror (maybe I am not using the right word for it...))?

This can (and should) happen in real-time, so it would be happening every frame to calculate the degree from where the camera is located to get the perspective of the camera in the mirror.

I have most of the things set up in a scene, such as a plane that has a render texture from the camera (parented to the center of the plane). I also have a main camera that may move anywhere on the reflective side of the mirror. The plane is also inversely scaled in the X axis to give the illusion that it is a mirror (making it synthetic (and useful in some cases), but I am looking to see if it can be realistic).

Is there anything I am missing from this problem? I would not be surprised if I forgot the positions of some random object or something like that. I think there is some sort of math algorithm out there that can calculate an angle from a plane (much like a line bouncing off a wall, if that makes sense). Any help and guidance is appreciated.

Some other pieces of information: I have Unity Pro 4.6.1 If I miss any piece of information, let me know and I will add that if I can.

Here are some attempts I have made on trying to turn the camera for this mirror (notice the comments as I continue to try out different things and add them here).

 void Update()
     {
         Vector3 targetDir = Vector3.Reflect(lookingCamera.transform.position, Vector3.forward) - transform.position;
         Vector3 forward = transform.forward;
         float angle = Vector3.Angle(targetDir, forward);
         if(debugMode)
         {
             Debug.Log(angle);
         }
         
         offset.x = lookingCamera.transform.position.x + adjustmentX;
         offset.y = -lookingCamera.transform.position.y + adjustmentY;
         
         //transform.Rotate(Vector3.forward, angle); 
         //reflectedObject.position = Vector3.Reflect(originalObject.position, Vector3.right);
         //thisCamera.transform.position = Vector3.Reflect(lookingCamera.transform.position, mirror.transform.position); 
         //transform.LookAt(Vector3.Reflect(lookingCamera.transform.position, mirror.transform.position));
         //transform.LookAt(Vector3.Reflect(lookingCamera.transform.position, Vector3.right));
         //Vector3.Reflect(lookingCamera.transform.position, Vector3.forward);
         
         
         //transform.LookAt(1 * Vector3.Reflect(lookingCamera.transform.position, Vector3.forward));
         //transform.LookAt(Vector3.Reflect(lookingCamera.transform.position, Vector3.up));
         transform.LookAt(offset + Vector3.Reflect(lookingCamera.transform.position, new Vector3(normalX, normalY, normalZ)));
         
         //transform.rotation *= Quaternion.Euler(0, 0, 90);
     }

Edit: I have also attached two pictures of what I am experiencing as of right now. I have also updated the block of code above.

Edit 2: It is getting close to being solved, I just don't understand on why the X axis is not working, but the Y-axis is working as expected. hmm... Maybe it has something to do with the offset I am adding?

mirrorerror01.jpg (39.3 kB)
mirrorerror02.jpg (42.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Jan 24, 2015 at 09:48 PM

http://docs.unity3d.com/ScriptReference/Vector3.Reflect.html

using this, the incoming is the sight of the player aka its forward, the normal is the mirror plane. Then you rotate the camera to align with the result.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AmasterAmaster · Jan 25, 2015 at 07:15 AM 0
Share

Ok, I will try that right now and I will get back to you if it works or not. Thanks for the help.

avatar image AmasterAmaster · Jan 25, 2015 at 05:49 PM 0
Share

Ok, after some testing and research, I am getting this wried effect. Sure, it works great, but it has the problem: As the player's sight get's closer to the right, the mirror camera shifts to the perspective of the player's sight (and still looks at the player's sight), when it should be looking at where the angle is supposed to be pointing at (and vice versa). hmm... Here is what I have inside the update function:

transform.LookAt(Vector3.Reflect(lookingCamera.transform.position, Vector3.forward));

am I doing something wrong here? ($$anonymous$$aybe I should store it next? hmm...)

I have also attached pictures of what I am experiencing in this problem.

mirrorerror01.jpg (39.3 kB)
mirrorerror02.jpg (42.7 kB)
avatar image fafase · Jan 26, 2015 at 10:39 AM 0
Share

I would think the first parameter is wrong and should be

   lookingCamera.transform.forward.

See now you are passing the vector from origin to the position of the camera (this is what position is). But what you want is the vector from the position in the forward of where you are looking. While the previous does not matter where you are looking at, only where you are.

Also the second parameter is also kinda wrong if the mirror is not aligned with the world coordinates, so you may want to use the forward again of the plane. Note that here, it might not be transform.forward since it will depend on the rotation of your object. What I mean is that you need to pass the vector pointing out of the mirror (which might actually transform.forward). Am I clear enough here?

so:

  transform.LookAt(Vector3.Reflect(lookingCamera.transform.forward, transform.forward));
avatar image AmasterAmaster · Jan 27, 2015 at 03:41 AM 0
Share

Alright, I will keep playing around with vectors and perspectives until I get it down. I thank you for the help and I will go ahead and accept this as the answer. And also I will study this as research along with trial and error testing to get this. Thank you once again.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Smooth Follow Camera that Keeps Up 0 Answers

Trouble with Camera Rotation Math 2 Answers

RTS Camera not working as expected 1 Answer

How to make camera position relative to a specific target. 1 Answer

Grab'n drag camera? (or, a little geometry problem) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges