- Home /
Identifying/detecting whether an object is visible in a mirror reflection (using Water.cs)
Hey guys,
I am using the Water.cs script in Reflective mode as a mirror and was wondering if it's possible to detect when an object is visible (being reflected) by the plane. I can see that the script uses a LayerMasks so I can choose which layers are culled, but is there a way to check if a specific object is being drawn?
I have tried using the regular Visibility checkers (OnBecameVisible/OnBecameInvisible as well as the IsVisibleFrom script on the Wiki) but as Water.cs uses cameras in an unconventional way these don't seem to work.
Any help would be great. Thanks in advance!
Not sure if this will work in your case, but here's a script that can be used to check if an object with the tag Target is seen by a specific camera:
#pragma strict
private var mirrorCam : Camera;
function Start()
{
mirrorCam = GetComponent.<Camera>();
}
function Update()
{
if (GameObject.FindWithTag("Target"))
{
var screenPoint = mirrorCam.WorldToViewportPoint(GameObject.FindWithTag("Target").transform.position);
if (screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1)
{
Debug.Log("Visible");
}
else
{
Debug.Log("Invisible");
}
}
else
{
Debug.Log("Invisible");
}
}
Answer by asimov · Jan 20, 2016 at 06:25 PM
Thanks for the script, ZefanS. However, it only checks if the object is on screen in the usual way. That is, if the object is in front of the camera. If we have a mirror placed in front of the camera which reflects an object which is at the camera's side (so not within the camera's view frustum), it will fail the screenpoint check you have set up.
I suspect one way would be to somehow detect which objects are being reflected by the Cameras in the m_ReflectionCameras dictionary in the Water.cs script. However, how to approach this is a little beyond my level of experience - any suggestions would be great.
Your answer
Follow this Question
Related Questions
Reflect the whole picture to the slope 1 Answer
Reflection/Refraction water 1 Answer
Mirror material in Indie? 4 Answers
mirror effect reversed side? 1 Answer
Mirror reflection stretching problems 0 Answers