Search Unity

Problem with two camera views and a button

Discussion in 'Scripting' started by herbie, Feb 15, 2013.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    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.
     
  2. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    is your button a GUI button?
     
  3. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    No, it no GUI button but a gameobject.
    When you click on the gameobject (with Input.GetMouseButtonUp(0)) a bullet is fired.
     
  4. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    I'm trying to understand why this is not working.

    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.
     
  5. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    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.

    Does anybody know a better solution?
     
  6. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    I am guessing you are using a raytrace to touch the button?

    if so the raytrace uses a camera so if you want to test both cameras you will need to do a second raytrace for the other camera.
     
  7. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Thanks for reply.

    I have attached the next script to the button (gameobject)

    Code (csharp):
    1.  
    2. var button : Collider;
    3.  
    4. function Update ()
    5. {
    6.     var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    7.     var hit : RaycastHit;
    8.    
    9.     if(Input.GetMouseButtonUp(0))
    10.     {
    11.         if (button.Raycast(ray, hit, Mathf.Infinity))
    12.         {
    13.             // do something
    14.         }
    15.     }
    16. }
    That is working perfect but only for the Main-camera-view.
    Is this what you call a raytrace?
     
  8. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    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
    Code (csharp):
    1.  
    2. var button : Collider;
    3.  
    4. function Update ()
    5.     {
    6.     var sceneCams =  Camera.allCameras; //returns a list of all cameras in the scene
    7.     for(var thisCam : Camera in sceneCams)
    8.         {
    9.         DoCameraRayCasts(thisCam);
    10.         }
    11.     }
    12.  
    13. function DoCameraRayCasts(camReference : Camera)
    14.     {
    15.     var ray : Ray = camReference.ScreenPointToRay (Input.mousePosition);
    16.     var hit : RaycastHit;
    17.     if(Input.GetMouseButtonUp(0))
    18.         {
    19.         if (button.Raycast(ray, hit, Mathf.Infinity))
    20.             {
    21.             // do something
    22.             }
    23.         }
    24.     }
    25.  
     
  9. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Thanks! Now it's working great.
    But more important, I understand how it works.

    Thanks again.