- Home /
Change Camera on (Right) Mouse Click
Hello, I need help on this one. I'm trying to make a TPS. One of the basic features is if you hold RMB, the camera is switched to another camera. Now, I'm very bad at coding so I'll need your help. Would you kindly...?
Answer by Freaking-Pingo · Mar 21, 2013 at 01:26 PM
Could this code be useful?
public Camera camera1;
public Camera camera2;
void Update(){
if (Input.GetMouseButtonDown(1)){
camera1.gameObject.active = false;
camera2.gameObject.active = true;
} else
{
camera1.gameObject.active = true;
camera2.gameObject.active = false;
}
}
Then you just have to assign camera1 and camera2 in the inspector on the object you assign this script on.
It was useful, but where do I apply the script? I applied it to both cameras and set the same components. the game launches fine but when i click R$$anonymous$$B, it turns to camera 2 for a split second, then back to camera 1.
That's because the Input.Get$$anonymous$$ouseButtonDown(1)
only returns true if the right mouse button is held down constantly. Try this :
if(Input.Get$$anonymous$$ouseButtonDown(1)){
camera1.gameObject.active = false;
camera2.gameObject.active = true;
} else if(Input.Get$$anonymous$$ouseButtonDown(0))
{
camera1.gameObject.active = true;
camera2.gameObject.active = false;
}
Edit : Now that I look at your question the code by @Freaking-Pingo is actually correct. You said that you wanted the camera to shift only on holding down the R$$anonymous$$B.
Thanks for the help, guys, I appreciate it a lot. Though, a final question, what gameobject do I put the script to? I hope I'm not being a bother.
Add the script for both cameras. Also use this code for initializing the camera objects :
camera1 = GameObject.Find(<Insert Camera 1 Gameobject Name>).GetComponent<Camera>();
camera2 = GameObject.Find(<Insert Camera 2 Gameobject Name>).GetComponent<Camera>();
It is not necessary to do what @VivekD mentioned, if you just assign the values in the inspector.
1: Attach your script to any gameobject on the scene 2: Select the game object you attached your script to 3: To the right in the inspector, you can see your script. Underneath that script you can see two grey boxes, with a little circle on the right. Click this circle and select your cameras, or you can simply drag and drop your cameras from the scene on top of those grey boxes.
Your answer
Follow this Question
Related Questions
Switching cameras with the click of the mouse 1 Answer
Camera Orbit on Left Click problem 0 Answers
Strange Camera Behavior on iOS - Runs perfect in Unity 0 Answers
Can't shoot towards mouse click point 1 Answer
Right Mouse Click in Android 2 Answers