- Home /
UnityEngine.Input.GetMouseButton(1)) issue
Hi i found this code online
var camera1 : Camera; var camera2 :
Camera; function Start () {
camera1.enabled = true;
camera2.enabled = false; }
function Update () { if
(Input.GetKeyDown ("2")){
camera1.enabled = false;
camera2.enabled = true; } if (Input.GetKeyDown ("1")){
camera1.enabled = true;
camera2.enabled = false; } }
which basically changes the camera in game and it works fine but i tried to alter it so instead of pressing the key 2 or 1 i wanted to make it so when you right click it changes so i changed the code so instead of it saying Input.GetKeyDown(''1'')) i changed it to say UnityEngine.Input.GetMouseButton(1)) but in game it dosent work when i right click heres the code that was altered
**
var camera1 : Camera;
var camera2 : Camera;
function Start () {
camera1.enabled = true;
camera2.enabled = false;
}
function Update () {
if (UnityEngine.Input.GetMouseButton(1)) {
camera1.enabled = false;
camera2.enabled = true;
}
if (UnityEngine.Input.GetMouseButton(1)) {
camera1.enabled = true;
camera2.enabled = false;
}
}
Thanks
Answer by robertbu · Feb 01, 2014 at 07:17 AM
You have a couple of problems. The first is that you want GetMouseButtonDown() instead of GetMouseButton(). GetMouseButton() will return true for as long as the key is pressed. You only want the change to happen once in the frame the key is pressed. As for the rest, if you look at the original code, it uses a different key for each camera setup. You are trying to use the same mouse button for both cameras. If you want just a single mouse button, you could do it this way:
var camera1 : Camera;
var camera2 : Camera;
function Start () {
camera1.enabled = true;
camera2.enabled = false;
}
function Update () {
if (Input.GetMouseButtonDown(1)) {
camera1.enabled = !camera1.enabled;
camera2.enabled = !camera1.enabled;
}
}