In my game I have a Main camera and a second camera. The second-camera-view is a part of the view of the Main camera but zoomed in. On the part of the Main-camera-view that is zoomed in, you see a button (Input.GetMouseButtonUp(0)).
So on the second-camera-view you see the same button as you see on the Main-camera-view but zoomed in.
The problem now is that you only can click on the button in the Main-camera-view.
The same (zoomed in) button on the second-camera-view does not react on a mouseclick, but that is what I would like to have.
How can I solve that?
The Main camera has a depth of 0 and the second camera has a depth of 1.
I have the script with Input.GetMouseButtonUp(0) attached to the button.
The button on the Main-camera-view is the real button. The button on the second-camera-view is a reproduction of the real button.
It seems that you only can click on the real button. But what I would like to have is that you can click on the reproduced button.
I still don’t know how to solve this properly.
What I can do is lay a GUI button over the second camera window but I don’t think that’s the right solution.
I have attached the next script to the button (gameobject)
var button : Collider;
function Update ()
{
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Input.GetMouseButtonUp(0))
{
if (button.Raycast(ray, hit, Mathf.Infinity))
{
// do something
}
}
}
That is working perfect but only for the Main-camera-view.
Is this what you call a raytrace?
Yes this is a ray trace… at line 5 you can see the reference to Camera.main which returns the first enabled camera tagged “MainCamera”.
In your case you would need to make an additional ray for the second camera you have. So you’d obtain a reference to the second camera using one of the Find methods, or use Camera.allCameras which would return ALL camera’s in scene or setting it directly to a global that you set from the inspector.
I’d then rewrite the Update ray caster into a function raycaster
var button : Collider;
function Update ()
{
var sceneCams = Camera.allCameras; //returns a list of all cameras in the scene
for(var thisCam : Camera in sceneCams)
{
DoCameraRayCasts(thisCam);
}
}
function DoCameraRayCasts(camReference : Camera)
{
var ray : Ray = camReference.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Input.GetMouseButtonUp(0))
{
if (button.Raycast(ray, hit, Mathf.Infinity))
{
// do something
}
}
}