I’ve been trying for the last…I dont know how many hours… to figure out a way to choose a random camera (from a list of cameras) to use when the main camera is off but am not having much luck at all. Can someone smarter than me give me a hand figuring this out?
var camera01 : Camera;//main camera
var camera02 : GameObject[];//random camera
static var camera1enabled : boolean = true;//main camera
function Update()
{
var obj : GameObject = camera02[Random.Range(0,camera02.length)];
if(camera1enabled)
{
camera01.enabled = true;
obj.enabled = false;
}
else
{
camera01.enabled = false;
obj.enabled = true;
}
}
Everything I try gives me errors and I just cant figure it out.
Using a switch seems a bit silly, you can’t scale it dynamically.
var randomCameras : GameObject[];
var mainCamera : GameObject;
function Update()
if (mainCamera.active)
return;
randomCameras[Random.Range(0, randomCameras.Length)].active = true;
}
Just shove your main camera in mainCamera, and however many cameras you want to randomly select in randomCameras
I took out the static bool as it seemed like a terrible programming practice. Just get a reference of the main camera and turn it off from any other script and a new camera will auto be selected.
You will need to maintain a constant timer on this. The easiest way is to run it in a coroutine. Check to see if the main camera is active, then if it is disable the rest of the cameras. Else enable the active camera only.
var randomCameras : GameObject[];
var mainCamera : GameObject;
var cameraSwap=3.0;
function Start()
var current : int=Random.value * randomCameras.Length;
var swap=Time.time + cameraSwap;
while(true){
for(var i=0; i<randomCameras.Length; i++)
randomCameras.active=false;
if (!mainCamera.active){
if(Time.time > swap){
current=Random.value * randomCameras.Length;
swap=Time.time + cameraSwap;
}
randomCameras[current].active=true;
}
yield;
}
}
I’m sorry to drag the conversation down to my level… But I don’t understand BigMisterB’s use of while(true). While what is true? I’m finding it hard to look up the usage for the word “while”. Then there’s the yield. Every help reference I’ve seen so far has a value associated with it: yield WaitForSeconds (5); How is His magic possible?
ah… while(true)… means… it is never false… it continues until the object is destroyed.
Oh, and yield WaitForSeconds(X); would not give you a good result because you don’t want it to check every X seconds… you want it to check every frame.
And yes… the magic is possible because of all the core monobehavior functions… Start may act as a Coroutine. This means yield will work here.
I just can’t wrap my Chattanooga head around this. What exactly is yield doing? It sure seems like “while” is an endless loop that should hang the system right there, except it hits the yield and just voodoos all your problems away! I understood yield to be a way of marking a line of code and coming back to it at some stated time in the future.
yield forces the program to display a frame (or in the case of wait for seconds a number of frames) before allowing the script to continue. and yes while(true) is endless. (Or should I say, it will end when the object no longer exists) This means it will persist until A) the program ends or B) the object is destroyed.
yield is used for things that are IEnumerator based meaning in this case when you use a while loop, Unity javascript automatically treat start as a IEnumerator based function and while loop does loop forever however the yield statement simple is just like the return statement, in this case yield null.
If thats too advanced, you need to know that in a while loop, yield is needed, in a coroutine, a yield is needed and when using things like WaitForSeconds().
you know instead of having a array of cameras you can use Camera.allCameras instead if all cameras in the scene are supposed to be checked. Also if your using all cameras make sure you get the value once only. Every time you call/query that property it makes a new array. So instead of
var camera = Camera.allCamera[Random.Range(0, Camera.allCameras.Length)];
you’d want
var allCameras = Camera.allCameras;// get the array once
var camera = allCameras[Random.Range(0, allCameras.Length)];
Yea, is a array. but its not a member so if he wanted to add more cameras to the scene or reuse it in other scenes he wouldnt have to configure in the instigator for each object / scene.