- Home /
How to change camera when mouse button is clicked?
I have multiple camera attached to separate empty game objects that surround the player. I want the camera to switch in order when the user presses the middle mouse button. However unity is not recognizing any of the objects in my scene. I'm guess I am forgetting to call something. Any help would be great!
//Player is the gameObject which holds the camera
Player.camera.active = false;
var CameraNum : int;
CameraNum = 0;
function Update ()
{
if(Input.GetMouseButtonDown(2))
{
CameraNum+=1;
Debug.Log ("You hit middle mouse button");
}
if(CameraNum == 1)
{
//CameraOne is an empty game object that is paired with a camera
CameraOne.camera.active = true;
Debug.Log ("Camera One active");
}
if(CameraNum == 2)
{
//CameraTwo is an empty game object that is paired with a camera
CameraOne.camera.active = false;
CameraTwo.camera.active = true;
Debug.Log ("Camera Two active");
}
if(CameraNum == 3)
{
//CameraThree is an empty game object that is paired with a camera
CameraTwo.camera.active = false;
CameraThree.camera.active = true;
Debug.Log ("Camera Three active");
}
}
Here, this should work:
//Player is the gameObject which holds the camera
//This is a list of all the cameras
var CameraOne : Camera;
var CameraTwo : Camera;
var CameraThree : Camera;
var PlayerCamera : Camera;
//This defines the PlayerCamera (to define player camera, find the player's camera under the Player(First Person Controller or third) and drag the camera into the object holding the script )
PlayerCamera.enabled = false;
var CameraNum : int;
CameraNum = 0;
function Update ()
{
if(Input.GetButtonDown("<your_key_here>"))
{
CameraNum ++ 1;
Debug.Log ("You hit middle mouse button");
}
if(CameraNum == 1)
{
//$$anonymous$$ake a camera called CameraOne, same for Two and Three
CameraOne.camera.enabled = true;
Debug.Log ("Camera One active");
}
if(CameraNum == 2)
{
//$$anonymous$$ake a camera called CameraTwo, same for One and Three
CameraOne.camera.enabled = false;
CameraTwo.camera.enabled = true;
Debug.Log ("Camera Two active");
}
if(CameraNum == 3)
{
//$$anonymous$$ake a camera called CameraThree, same for One and Two
CameraTwo.camera.enabled = false;
CameraThree.camera.enabled = true;
Debug.Log ("Camera Three active");
}
if(CameraNum > 3){
//This resets the camera number variable every time the variable is greater than 3
CameraNum == 1;
}
}
@TheRichardGamer: I already said the thing with enabled and active.
@Damich123: Is your problem solved now because if that's the case i would convert my comment to an answer so that you can accept it to mark the question as solved.
Answer by Mannis · May 20, 2013 at 08:51 PM
Have you considered using a single camera, and applying new positions/rotations on each MMB click?
Answer by TheRichardGamer · May 21, 2013 at 02:15 PM
Modify your inputs to be for example "MiddleMouse" to be the middle mouse button by going into Edit > Project Settings > Input, and then modify all your inputs. Then once you have done that you can just replace if(Input.GetMouseButtonDown(2)) to if(Input.GetButtonDown("")) That would make things a bit easier. And also change the active term to enabled = true or = false depending on what you would like to activate/deactivate. I hope this helps :)
Your answer
