Camera Switch

Hey, Im a beginner and ive written this script for switching from camera1 to camera2 (which is a third person camera) but it dosent work. I need help please.

using UnityEngine;

public class CamSwitch : MonoBehaviour
{

    public GameObject cameraOne;
    public GameObject cameraTwo;

    bool thirdPerson = false;

    void Update()
    {
       
   

        //Set camera position 1
        if (thirdPerson == false)
        {
            if (Input.GetKeyDown(KeyCode.C))
            {
                cameraOne.SetActive(false);
                cameraTwo.SetActive(true);
                thirdPerson = true;
            }
        }
      

        //Set camera position 2
        if (thirdPerson == true)
        {
            if (Input.GetKeyDown(KeyCode.C))
            {
                cameraTwo.SetActive(false);
                cameraOne.SetActive(true);
                thirdPerson = false;
            }
        }

    }
}

What does “doesn’t work” mean specifically? From a quick glance though you should use an If/else instead of two if’s on lines 17 and 29. As written if thirdPerson is false when Update starts and C is pressed, the following will occur:

  1. Lines 17 and 19 resolve to True
  2. Cameras are switched on lines 21 and 22
  3. thirdPerson is set to True
  4. now that thirdPerson is true lines 29 and 31 resolve to True
  5. The cameras are switched back on lines 33 and 34
  6. theirPerson is set back to false on line 35, ending right back where you started

So do and if/else so you can change the value of thirdPerson without the above occurring

if (thirdPerson == false)
{
//blahblah
}
else
{
//more blahblah
}

Please read this post:

http://plbm.com/?p=220

… essentially what Uncle Joe identified above.

1 Like