Hi! I need a script for my cameras to switch between them automatically. For example, It shows camera1 and in 5 seconds It changes to camera2.
Thanks.
Hi! I need a script for my cameras to switch between them automatically. For example, It shows camera1 and in 5 seconds It changes to camera2.
Thanks.
in c#:
create the two Camera variables (public so they can be dragged in). this is outside any method/function
Camera cam1, cam2;
then, we need to set one of them enabled, and the other disabled. when it’s disabled it is not used
this is in the premade Start method
void Start() {
cam1.enabled = true;
cam2.enabled = false;
}
in order to have a delay, we need to do WaitForSeconds()
in C# it’s a little more strange than in javascript
we need to make a coroutine through an IEnumerator
so, outside of the methods
the timeDelay is the time (in seconds) that you want to wait for. you can change the name of the coroutine (which is changeCam in this instance) to anything you want, just be sure to change the calling name too
IEnumerator changeCam(float timeDelay) {
//before the time delay
//this waits the amount that the method was called with
yield return new WaitForSeconds(timeDelay);
//after the time delay... let's say it was 5 seconds, so the stuff after the waitforseconds is called only after that 5 seconds, and then the stuff before isn't called again.
//you can incorporate this some easier way, but I'm lazy so ill just make it change the cameras in here
//so:
cam1.enabled = false;
cam2.enabled = true;
//then we need to stop this coroutine so it doesn't just keep going and going and taking up time
yield break;
//this just stops the coroutine immediately
}
But wait, we forgot something! We never ever started the coroutine. So, inside our premade Start(), we add after the enabling and disabling of the cameras:
so like you said in your example, you want to delay by 5 seconds, so let’s call our coroutine with a 5 second delay in it’s parameters
StartCoroutine(changeCam(5));
You only need to call coroutines once (here we did it in the start), because they keep repeating within themselves
This is untested code but i’m like 99% sure it’s all right. If I messed up or you have any questions just ask!
can you write it properly?
Thanks it helped me a lot I was searching for it only