- Home /
Changing Cameras With One Button
Hi, I have setup in my game a script which changes the cameras with different buttons, but I want to make it so that I can use the same button to change between cameras, instead of using 2 buttons. How could I solve this problem?
Current Camera Script
public var rearCamera : Camera ;
public var driverCamera : Camera ;
function Start() {
rearCamera.camera.enabled = true;
driverCamera.camera.enabled = false;
}
function Update() {
if (Input.GetKey(KeyCode.I))
{
rearCamera.camera.enabled = false;
driverCamera.camera.enabled = true;
}
else if (Input.GetKey(KeyCode.O))
{
driverCamera.camera.enabled = false;
rearCamera.camera.enabled = true;
}
}
Answer by justin35f · Jan 19, 2013 at 04:01 AM
I just ran this and it works pretty well I think.
#pragma strict
public var rearCamera : Camera ;
public var driverCamera : Camera ;
function Start() {
rearCamera.camera.enabled = true;
driverCamera.camera.enabled = false;
}
function Update() {
if(Input.GetButtonDown("CameraSwitch"))
{
rearCamera.camera.enabled = !rearCamera.camera.enabled;
driverCamera.camera.enabled = !driverCamera.camera.enabled;
}
}
I used Input.GetButtonDown, because if you use GetKey it will detect a button press every from that the button is down. So unless you have lightning fingers, or want to swap camera's every frame, this isn't ideal. You will need to go to Edit/Project Settings/Input then expand 'Axes'. Increase size by one. This will copy the last axes. Rename it, and set the 'Positive Button' to the button you want to be pressed. In the GetButtonDown function, pass in whatever you named the axes to.
If you need further help, or if this is not the solution you are looking for, let me know.
Thanks, it is working perfectly :D, but could you help me adding another camera? This one should be only active while a button is pressed, otherwise it returns to the previous camera. Something like this :
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.O)) { reverseCamera.camera.enabled = true; rearCamera.camera.enabled = false; driverCamera.camera.enabled = false; }
No problem. It's actually pretty simple. Add this in your variable declarations:
public var reverseCamera : Camera;
Now add this into the Update function.
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.P)) { reverseCamera.camera.enabled = true; } else { reverseCamera.camera.enabled = false; }
My Errors:
Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(15,27): error CS1003: Syntax error, ',' expected
Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(15,29): error CS1002: ; expected
Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(15,35): error CS1519: Invalid token ';' in class, struct, or interface member declaration
Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(16,26): error CS1003: Syntax error, ',' expected
Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(16,28): error CS1002: ; expected
Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(16,34): error CS1519: Invalid token ';' in class, struct, or interface member declaration
My Script: public class PlayerWeapon : MonoBehaviour {
public GameObject bulletPrefab;
public Transform bulletSpawn;
public float bulletSpeed = 30;
public float lifeTime = 3;
public var mainCamera : Camera;
public var gunCamera : Camera;
void Start()
{
mainCamera.camera.enabled = true;
gunCamera.camera.enabled = false;
}
void Update()
{
if(Input.GetKey(KeyCode.Mouse1))
{
mainCamera.camera.enabled = !mainCamera.camera.enabled;
gunCamera.camera.enabled = !gunCamera.camera.enabled;
}
if (Input.GetMouseButtonDown(0))
{
Fire();
}
if (Input.GetKey(KeyCode.Mouse2))
{
Fire();
}
}
Hi, before answering, please be nice when asking for help, saying something like "I've tried this code but is not working, can you please help me?". Just posting a wall of errors and your code hoping someone will answer is rude and this site's community is built on good manners and etiquette.
Anyhow, I think your problem is derived from a language confusion, since when I asked this question a long time ago, it was programmed in UnityScript, while you are running C#. In C#, the var keyword can only be used inside methods, not to declare class variables. For example:
void MyMethod()
{
var tempVar = 3;
}
To fix your error, you have to specify what type of variable the cameras are, which in the long run is a good practice as it makes code much easier to understand and helps avoid undesired errors. Your variables should be like this:
public Camera mainCamera;
public Camera gunCamera;
Hope this helps you and allows you to move forward with your game. Good luck!
Answer by eXtremeTY · Jan 19, 2013 at 02:58 AM
Not tested, but should work.. ;)
public var rearCamera : Camera ;
public var driverCamera : Camera ;
public toggle : boolean = false;
function Start() {
rearCamera.camera.enabled = true;
driverCamera.camera.enabled = false;
}
function Update() {
if (Input.GetKey(KeyCode.I) && !toggle) //<---- enable rear camera
{
rearCamera.camera.enabled = true;
driverCamera.camera.enabled = false;
toggle = true;
}
else if (Input.GetKey(KeyCode.I) && toggle) //<---- enable driver camera
{
rearCamera.camera.enabled = false;
driverCamera.camera.enabled = driver;
toggle = false;
}
}
Hi, thanks for your answer, but it has a little "bug", sometimes when I press the button it changes fo a milisecond to the other camera and then returns to the other, do you know how to fix it?
I just seen it now, sorry, was away.. I'll just leave it as it is seeing as someone else helped you out. :) Good luck!
Answer by iahr · Jul 26, 2016 at 08:06 AM
Try this code. I make this code switching to 2 cameras. using UnityEngine; using System.Collections; public class CameraManagerScr : MonoBehaviour { %|1051292963_1|% %|446312010_2|% %|794809699_3|% %|1039055902_4|% %|-552888617_5|% %|977180863_6|% %|-934038197_7|% %|1782251902_8|% %|-1412472038_9|% %|-813138912_10|% %|-21545877_11|% %|-207334728_12|% %|837798027_13|% %|-804862433_14|% %|270663048_15|% %|-1892770785_16|% %|-1459013006_17|% %|1742211472_18|% %|-1850636563_19|% %|-1519131145_20|% %|1347906305_21|% %|901506400_22|% }
Answer by zenforhire · May 18, 2021 at 06:34 PM
Your case only works because there is ONLY one object interested in the toggle. What happens when there are 5 objects interested in the same toggle?
We created a Toggle class that sends events when toggled. Objects interested in it toggling listen for the event. The button OnClick calls the toggle method on the Toggle class. The button does NOT need to know who is interested in the change.