- Home /
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?
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.
Ok, I will try that right now and I will get back to you if it works or not. Thanks for the help.
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.
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));
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.