Jimmy's tutorials are made for absolute beginners, so it's good to take his code, and see how you can improve on it.
CREATE DIFFERENT CAMERA OBJECTS
CREATE C# SCRIPT
You'll need to create a C# script to effectively enable and disable the camera objects. Below is a drastically modified version from the one in the tutorial.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraChange : MonoBehaviour { public GameObject[] camModes; private int currentCamMode; private bool buttonPressed = false; // Update is called once per frame private void Start() { currentCamMode = 0; } void Update() { if (Input.GetButton("viewmode") && !buttonPressed) { buttonPressed = true; StartCoroutine(CamChange()); } } IEnumerator CamChange() { yield return new WaitForSeconds(0.5f); camModes[currentCamMode].SetActive(false); currentCamMode++; if (currentCamMode == camModes.Length) { currentCamMode = 0; } camModes[currentCamMode].SetActive(true); buttonPressed = false; } } |
camModes: Instead of creating three separate variables for each camera, I just created an array to hold them all.
buttonPressed: This variable is important and didn't exist in the tutorial. Without this variable, the camera would change would trigger multiple times per frame, making it very glitchy. Basically, in the update method, if that variable is true, it will not try to change the camera again.
Start(): I just use this method to set the current camera to 0 (NormalCam). This method only gets called once, so this is a good place to set default variables.
CamChange(): First we wait half a second (adjust this as you see fit). Then we disable the camera we're currently using (currentCamMode). We then increment currentCamMode by 1, so if we were on NormalCam(0), we'd now be on FarCam(1). The next line prevents an "Index out of bounds" error from occurring and resets the currentCamMode variable to 0. With our updated currentCamMode variable, we use it to enable the next camera object. And finally, we set the buttonPressed variable to false, which allows us to switch camera angles again.
ADD SCRIPT TO EMPTY OBJECT
Create an empty object and call it whatever you want, something like CameraManager. Take that above script, and drag it onto that object. You should see a camModes variable with a size parameter. Set the size to 3 or however many camera objects you have.Finally, just drag each camera object into each element. That's it! Assuming you have your "viewmodes" key set, all should work. If you're having trouble, let me know in the comments, and I'll help you out.