Search Unity

Selecting Cameras

Discussion in 'Scripting' started by marty, Jun 12, 2005.

  1. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    If I have multiple cameras in a scene, how do I select/change the rendering camera in JavaScript?
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Well, I'd assume that a camera is "rendering" as long as the GO is enabled. A camera is a subclass of Behavior, so check out the Behavior docs:

    file:///Applications/Unity/Documentation/ScriptReference/Behaviour.html

    As you can see there is an "enabled" property. You'd just want to enable/disable those accordingly.

    Now looking at the Camera documentation...

    file:///Applications/Unity/Documentation/ScriptReference/Camera.html

    There is a "depth" property which determines which cameras get rendered first. You could also just switch those around, but that's a bit ugly. (Don't want to be rendering stuff you don't see if possible)

    Also, notice there is an allCameras property to the class, if you need to get a list of all cameras to figure out if you want them enabled or not.

    It's all in the docs

    HTH,
    -Jon
     
  3. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Man, that documentation will sure be a lot more helpful once each entry in it has:

    1. A complete prose description
    2. A syntax example

    :wink:

    For now, could anyone provide a line or two of JavaScript code demonstrating how I switch cameras? That is to say, if I have a scene with two cameras in it, I'd like to see a line of code that will switch the camera being used to view the scene from camera number one to camera number two.

    Thanks!
     
  4. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Way back, this is what Joe sent to me for use with two cameras ... more can be added ...

    public class CameraUpdater : MonoBehaviour
    {
    public Camera camera0;
    public Camera camera1;

    void Update ()
    {
    if (Input.GetKey ("1"))
    {
    camera1.enabled = true;
    camera0.enabled = false;
    }
    if (Input.GetKey ("0"))
    {
    camera1.enabled = false;
    camera0.enabled = true;
    }
    }
    }

    Then in the editor you add this script to basically any game object and then you just assign the two camera properties to the cameras you want to enable or disable in the script behaviour you just added.
     
  5. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Oh, so it's just that little old "enabled" property.

    Thanks DaveyJJ!

    I wonder what happens if you leave both cameras "enabled"? Can we maybe somehow do a two camera view (i.e. split-screen)?
     
  6. David-Helgason

    David-Helgason

    Moderator

    Joined:
    Mar 29, 2005
    Posts:
    1,104
    O yes!

    The camera has settings telling it which area of the screen/window it should be at. So you can easily have little cameras on top of the big one.

    I attached a picture of a camera inspector. You need to set it to FULL (at the top of the inspector) to see the "Normalized View Port Rect" property. Just edit Xmin etc.

    Then use the Depth parameter to decide the order; the higher the number, the later it is drawn, so a camera with Depth 1 is on top of a camera with Depth 0.
     

    Attached Files:

  7. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
  8. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Woah! This is great - you guys thought of everything!

    Now, how do I make a camera that is "on top of" another semi-transparent? I've tried setting it's background color opacity to less than 100% but that doesn't seem to do the trick.
     
  9. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    You can control whether the background behind the camera is cleared by setting the Clear flags parameter to Depth only.

    The parameter gives you control of whether the background and/or depth information from the previous camera is cleared before rendering the next one.

    Thus the different values have the following efects:
    • Don't clear - both the background and z-buffer is left alone. Which means that the result of the current camera will only show where the depth is less than the depth of the already rendered picture.
    • Depth only - the z-buffer is cleared, but the results of the last camera will show through instead of the background color or skybox.
    • Color only - The background is cleared, but the z-buffer is left alone... I can't really think of a use for that one... Someone will probalby use it to create fancy effects.
    • Color and depth - The default behaviour

    PS. ... and yes, I have noticed and filed the bug where the apostrophe in "Don't clear" is written as an XML entity.
     
  10. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Yeah, I played with those.

    Thing is, in all of those, the background on my "inset camera" (think "HUD") is either completely there or completely gone. None of those settings appear to allow me to have the background color of the inset camera semitransparent. I even tried setting the opacity of the camera's background color, with no change.

    Do I need to maybe have a background (pre-layer) image in that camera?
     

    Attached Files:

  11. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    ... or you could add a gui texture to the camera beneath... maybe...
     
  12. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    Hi All,

    may i know how u all get the the output as shown above? I need to do something like that for my project.

    i tired the code as "DaveyJJ" have posted earlier, however i still can't get the output. It show me error such as " unexpected token:public --> which refering to this statement on "public class CameraUpdater : MonoBehaviour"

    Anyway, i have attach this script to my maincamera and my main camera itself doesn't have a monobehaviour? Wonder is it because of this issue?

    Thanks.. :roll:

    Sotongqueen
     
  13. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    You need to change the Xmin, Ymin, Xmax and Ymax settings in the Inspector panel section that David attached in his post above.

    For some helpful hints, as always, just hit the Help question mark icon in the inspector panel for the camera near these settings.
     
  14. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    Thanks for the reply. Yes i did change the setting for Xmin, Ymin, Xmax and Ymax from the inspector but i still received the same error. May i check where did u attach the script to? For my case i am attaching it to my main camera focusing on a car in my terrain.

    Secondly, what do u mean by hitting the Help question mark icon in the inspector panel? do u mean the icon next to the play, pause function?
    or just click on the help from the taskbar above? Thanks
     
  15. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Did you perchance paste the script into a javascript file? The code snippet you show is C# code.

    Translated to Javascirpt it would look like this: (Save it to a file called CameraUpdater.js)
    Code (csharp):
    1. var camera0 : Camera;
    2. var camera1 : Camera;
    3.  
    4. function Update ()
    5. {
    6.     if (Input.GetKey ("1"))
    7.     {
    8.         camera1.enabled = true;
    9.         camera0.enabled = false;
    10.     }
    11.     if (Input.GetKey ("0"))
    12.     {
    13.         camera1.enabled = false;
    14.         camera0.enabled = true;
    15.     }
    16. }
     
  16. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Beat me to the punch. ;-)
     
  17. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    It's the small question mark (on a blue book) in the inspector visible in the title of every component (see attached image)
     

    Attached Files:

  18. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    i am really thankful for all your reply. Thanks. :)
     
  19. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    hi, i would like to check as i am using the following code

    var camera0 : Camera;
    var camera1 : Camera;

    function Update ()
    {
    if (Input.GetKey ("1"))
    {
    camera1.enabled = true;
    camera0.enabled = false;
    }

    such that when the user press 1, the camera 1 will appear and camera 0 will disappear.
    However, may i check is it possible to enlarge my camera1 size after when the user press on 1?

    Right now my camera 1 size is 600*400. However i want my camera 1 to be in 1024*768(full screen) when the user have finished pressing on 1. Can i achieve that?

    Thanks in advance