H there, Im just starting with unity and wanted to try some simple scripting. Ive set up a simple scene with two cameras and I wanted to set up a button to toggle between them I.e. I press “b” and i look through one camera, and I press “n” and look through the other, heres the code I tried to cobble together which unsurprisingly isn’t working, (I just attached the code to a game object assuming that that would make it evaluate?) any help would be greatly appreciated.
var cam1 = GameObject.FindWithTag("MainCamera");
var cam2 = GameObject.FindWithTag("testCam");
var screenRect = Rect(0,0,100,100);
function Update () {
if (Input.GetKey("b"))
{
DrawCamera(position:screenRect, camera:cam1, drawMode:smile:rawCameraMode=DrawCameraMode.normal):void;
}
if (Input.GetKey("n"))
DrawCamera(position:screenRect, camera:cam2, drawMode:smile:rawCameraMode=DrawCameraMode.normal):void;
{
}
}
Thats clever, i didn’t think of that. Would this approach still be efficient if each camera had a different behaviour, I.e. I have one camera as a first person camera and one as say a security camera?
i knew it had to be simple, in that case when you defined the variables like this:
var camera1:Camera;
var camera2:Camera;
What should ‘Camera’ be, should it be the name of the camera or the cameras tag, I’m not so sure on how to address these kind of things, its all new to me.
the :Camera specifies the type of the variable. Just like you have int, string, bool variables you can have “bigger objects” like camera.
Normally the type can be inferred by your assignment like the following.
var a = true;
//actually translates to
var a : Boolean = true;
var screenRect = Rect(0,0,100,100);
//translates to
var screenRect:Rect = Rect(0,0,100,100);
An added bonus from unity is that if you specify a type unity knows like camera you can visually set the variable inside the inspector. So if you put Cameras A,B and C into your scene you can select any of these cameras in the inspector as camera1 or camera2.
Wow thats totally genius, I love it. Now everything is working fine. Just out of interest if i wanted to address a camera directly from within the script how would I do that?
j
You could use Gameobject.Find to address the camera, but Find is not particularly efficient. If you know you’re going to use two particular cameras (or any gameobjects for that matter) it’s better to store them as variables. You could also assign the camera to a variable in the Start() function as well. That way you’re only finding the object once instead of on every button press.
By the way, if you want to toggle the cameras on a single button you can do this:
var camera1 : Camera;
var camera2 : Camera;
private var cameraSwitch : boolean = true;
function Update()
{
if (Input.GetKeyDown("n"))
{
cameraSwitch = !cameraSwitch;
camera1.enabled = cameraSwitch;
camera2.enabled = !cameraSwitch;
}
}
thanks for all the replies, I’m really impressed with how many and how quick the responses are. I’m getting the effect I want now which is amazing as i only installed unity yesterday! Although I’m begining to think that my js is a bit rough, im not sure exactly what the ‘:’ operator does for example, i’d better go and read some more.
Hey CheyTac. Just to let you know, declaring your cameras as Transforms isn’t going to work as you’ve coded it above. You’d have to drill down through GetComponent as in billykater’s example. You’re better off declaring cameras as:
var myCam : Camera;
Then you can address the camera component directly.
Hi, I really like the simplicity of tool55’s script and how it allows you to add the cameras directly in the inspector. I am trying to implement this using a 3 or more camera system. I’ve added a third variable and attached the camera in the inspector but it still only switches between two cameras. Is this because of the boolean? Can anyone see what I’m doing wrong?
Here is my script:
var camera1 : Camera;
var camera2 : Camera;
var camera3 : Camera;
private var cameraSwitch : boolean = true;
function Update()
{
if (Input.GetKeyDown(“n”))
{
cameraSwitch = !cameraSwitch;
camera1.enabled = cameraSwitch;
camera2.enabled = cameraSwitch;
camera2.enabled = !cameraSwitch;
}
}
With 3 cameras you’ll probably want to put them in an array. The script you posted switches between two cameras because the last line essentially undoes the line before it. Boolean’s are just true/false so only good for switching between two states or two objects, at least in this simple form. A built in array would work fine. Then iterate through them. I’ll try to post a script later if someone doesn’t beat me to it
Thank you for your quick reply tool55. Sorry, but by array do you mean that the cameras need to be assigned inside the brackets? :S
I’m still only learning scripting.
I have also tried another Multiple Camera system written in C# here →
However the problem with that one is that it activates the first camera automatically on runtime, whereas I need it to only activate when pressing the key. I’m using this to switch my player cameras and my vehicle cameras at separate times but using the same key “c”. I’ve put together a diagram →
Here is a basic example of a camera switcher… it allows you to press the “N” key to cycle through the list of cameras that you assign in the inspector. It also allows you to press a number and get a corresponding camera.
Its basic, but it illustrates an easy camera controller. It also contains error checkers, like if you don’t assign any cameras to the array, it wont work.
With the built in arrays you will see the array appear in the inspector. No need to put the cameras in the brackets. It will ask you for the array length (in your case 3, but it could be 100 or whatever) and you’ll instantly get a drop down list with empty camera slots. Just drag and drop your cameras into the slots to assign them to the array. BigMisterB’s switcher should be okay. You may not need all the if statements if you don’t want to assign separate keys on top of the N key. We’ll see if we can simplify it for you. Just no time this minute. I’ll try to look at it later today.