I’m in the very early stages of learning Unity and am currently on the second Unity Learn Pathway, “Junior Programmer.”
The first section of the pathway deals with a game where you make a car drive down a road and hit some boxes, at the end their are some additional challenges that are optional to complete, but for the sake of learning you’ve gotta take them on ya know.
One of the challenges is to make a second camera angle that you can switch to, on the hood of the vehicle, which was easy enough… but I wanted to set myself a second step to this and try and add a third, dynamic camera view, to prove to myself I understood the process and wasn’t just copying the code I was told.
The problem is I can’t get the key I’ve picked “F” to cycle through all three cameras properly. I’m pretty sure that in the way I’ve decided to do the scripting the problem is both IF statements are completed at the same time, with the mainCamera being disabled, the hoodCamera being enabled in the same instance the second IF statement checks for those requirements to switch to the dynamaCamera, meaning it skips from Camera 1 to Camera 3… I’m not sure exactly how to subvert this though, as I cant find a way to either delay the second IF statement’s checking or rewrite the code in a different way entirely. I’ll try my best to correctly show the important script below, but again I’m very new at this so it might not turn out correctly.
TLDR; I have three cameras I want to switch through with the “F” key, The Main Camera (mainCamera), The Hood Camera (hoodCamera), and The Dynamic Camera (dynamaCamera)… my IF statements are running at the same time, causing the “F” key to skip the Hood Camera, and I need there to be the tiniest delay between the IF statements checking for it’s requirements, or some other solution entirely.
I appreciate any help you can give me, whether it be one character or one hundred thousand… and I’ll finish off with again saying I am a Unity beginner, I’m sure the code sucks, I’m aware, thank you!
public Camera mainCamera;
public Camera hoodCamera;
public Camera dynamaCamera;
void Update()
{
if (Input.GetKeyDown(switchKey))
{
mainCamera.enabled = !mainCamera.enabled;
hoodCamera.enabled = !hoodCamera.enabled;
}
if (Input.GetKeyDown(switchKey) && hoodCamera.enabled)
{
dynamaCamera.enabled = !dynamaCamera.enabled;
hoodCamera.enabled = !hoodCamera.enabled;
}
}