Search Unity

Simple Camera Switch, Small bugs.

Discussion in 'Scripting' started by W4RH4WK117, Mar 11, 2012.

  1. W4RH4WK117

    W4RH4WK117

    Joined:
    Feb 6, 2011
    Posts:
    129
    Hey guys, I have some small bugs in this script and not sure how to fix it. There are two problems. The first one is that it never starts on the specified camera, if I set it to start on camera 1 it only ever starts on camera 2 no matter what I set it too. The next one is that when the scene first loads it requires a second key press before it starts switching so for example, I press c nothing happens then I press c again then it starts switching through. Also feel free to use the script on your own projects I just used this for a vehicle so I could switch to firstperson etc.

    Code (csharp):
    1. var camera1: Camera;
    2. var camera2: Camera;
    3. var camera3: Camera;
    4. var camera4: Camera;
    5. var LookBehind: Camera;
    6. public var startCamera : int = 1;
    7.  
    8. function start ()
    9.  
    10. {
    11. startCamera = 1;
    12. camera1.enabled = true;
    13. camera2.enabled = false;
    14. camera3.enabled = false;
    15. camera4.enabled = false;
    16. LookBehind.enabled = false;
    17.  
    18.  
    19. }
    20.  
    21. function Update ()
    22.  
    23. {
    24.  
    25. if (Input.GetKeyDown ("c")  (startCamera == 1))
    26.    
    27.    {
    28.       startCamera = 2;
    29.       camera1.enabled = false;
    30.       camera2.enabled = true;
    31.       camera3.enabled = false;
    32.       camera4.enabled = false;
    33.    }
    34.    
    35.    else if (Input.GetKeyDown ("c")  (startCamera == 2))
    36.  {
    37.       startCamera = 3;
    38.       camera1.enabled = false;
    39.       camera2.enabled = false;
    40.       camera3.enabled = true;
    41.       camera4.enabled = false;
    42.    }
    43.      
    44.       else if (Input.GetKeyDown ("c")  (startCamera == 3))
    45.    {
    46.       startCamera = 4;
    47.       camera1.enabled = false;
    48.       camera2.enabled = false;
    49.       camera3.enabled = false;
    50.       camera4.enabled = true;
    51.    }
    52.    
    53.    else if (Input.GetKeyDown ("c")  (startCamera == 4))
    54.    {
    55.       startCamera = 1;
    56.       camera1.enabled = true;
    57.       camera2.enabled = false;
    58.       camera3.enabled = false;
    59.       camera4.enabled = false;
    60.    }
    61.    
    62.    if (Input.GetKeyDown ("v"))
    63.    {
    64.       LookBehind.enabled = true;
    65.    }
    66.  
    67.    else if (Input.GetKeyUp ("v"))
    68.    {
    69.       LookBehind.enabled = false;
    70.    }
    71.  
    72. }
    Cheers
    WarHawk
     
    Last edited: Mar 11, 2012