Search Unity

[SOLVED] Camera change scripts blocks other camera scripts , Need help, I'm stuck in mud.

Discussion in 'Scripting' started by Pulov, Mar 2, 2015.

  1. Pulov

    Pulov

    Joined:
    Feb 20, 2010
    Posts:
    824
    Hi all ,

    I made this script to change betwen cameras, I used an array to be flexible enough for future camera number increase etc.

    The camera changer works great but it blocks any other script that is related to the cameras withing the script such could be a simple, press F to zoon that modiffies field of view. Other such mouse wheel zoom are also blocked, as soon as I disable this camera switch script all stars to work.

    PFFFF any ideas on what is going on?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class CameraCycle : MonoBehaviour
    6. {
    7.  
    8.     public GameObject  [] Cameras;
    9.     private int index = 0;
    10.     private GameObject activeCam;
    11.  
    12.     void Start()
    13.     {
    14.         activeCam = Cameras [0];
    15.     }
    16.  
    17.     void Update ()
    18.     {
    19.  
    20.         if (Input.GetKeyDown(KeyCode.Q))
    21.         index++;
    22.      
    23.         if (index>Cameras.Length-1)
    24.             index=0;
    25.  
    26.  
    27.         ViewChange();
    28.         Debug.Log (index);
    29.  
    30.     }
    31.  
    32.     void ViewChange ()
    33.     {
    34.     activeCam.SetActive (false);              
    35.     activeCam = Cameras[index];
    36.     activeCam.SetActive (true);
    37.  
    38.  
    39.  
    40.  
    41.  
    42.  
    43.     }
    44. }
     
    Last edited: Mar 2, 2015
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    It's probably because you are calling ViewChange function in Update, so it always sets the current cam false and then true, false and then true, thus making it to disfunction. You should be calling it with key press, try this :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class CameraCycle : MonoBehaviour
    4. {
    5.     public GameObject  [] Cameras;
    6.     private int index = 0;
    7.     private GameObject activeCam;
    8.     void Start()
    9.     {
    10.         activeCam = Cameras [0];
    11.     }
    12.     void Update ()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.Q))
    15.        {
    16.          index++;
    17.          ViewChange();
    18.        }
    19.      
    20.      
    21.         if (index>Cameras.Length-1)
    22.             index=0;
    23.      
    24.         Debug.Log (index);
    25.  
    26.     }
    27.     void ViewChange ()
    28.     {
    29.     activeCam.SetActive (false);            
    30.     activeCam = Cameras[index];
    31.     activeCam.SetActive (true);
    32.     }
    33. }
     
  3. Pulov

    Pulov

    Joined:
    Feb 20, 2010
    Posts:
    824
    Aaah men.

    Thank you so much!. You where totally right!.

    Now you people you ha've a simple clean camera switcher for your project ;)
     
  4. Pulov

    Pulov

    Joined:
    Feb 20, 2010
    Posts:
    824
    Hey, an small issue that generated your change is that the index was gettin out of range cause the second if was not being reached, so simply anyone interested in using this script place the second if withing the first one anda tadaa, now it works superb. Thanks again!.
     
    lineupthesky likes this.