- Home /
Switch camera with mouse
hi,
im currently using this to switch cameras
var cam1 : Camera;
var cam2 : Camera;
function Update () {
if(Input.GetKeyDown("1")){
cam1.enabled = true;
cam2.enabled = false;
}
if(Input.GetKeyDown("2")){
cam1.enabled = false;
cam2.enabled = true;
}
}
But instead of pressing a button, i want to change camera when i click on an object with the left mouse button
Then you'll need to look up Raycast and ScreenPointToRay in the scripting reference, put tags and colliders on your objects, and then Raycast where you mouseclick and read the tag of that object. Then you can select the camera based on the object tags. There are answers here on this site for each of these individual steps, so there's no need to recapitulate all of them...
Answer by The_r0nin · Jan 09, 2011 at 02:01 PM
Try OnMouseDown on your object scripts.
In the camera script:
var helperObject: GameObject; // assign this in the inspector
function OnMouseDown () { helperObject.SwitchCam(1); // or 2 or 3... whatever the number }
On your helper object's script:
var cam1:Camera; // assign these in inspector var cam2:Camera; var cam3:Camera;
function SwitchCam(newCam:int) { cam1.enabled = false; cam2.enabled = false; cam3.enabled = false; switch (newCam) { case 2: cam2.enabled = true; break; case 3: cam3.enabled = true; break; default: cam1.enabled = true; break; } }
Now, there are other good coding practices (like using enums instead of numbers, etc) that you can include, but this should get you started.
thanks for help but i thing im doing something wrong. Im getting a "expecting EOF, found helperObject" and a "expecting EOF, found cam1"
$$anonymous$$y bad, forgot I had switched to javascript for this. Try my newest edit...
BCE0019 switchcam is not a member of `unityengine.Gameobject
Capitalization matters in Unity script; case is important. It has to be "SwitchCam", not "switchcam." Functions start with a capital letter, and variables with a lower case letter (usually).
sorry its "BCE0019 SwitchCam is not a member of `unityengine.Gameobject" i copied and pasted it directly from your comment.
Answer by BillyGlenn · Jan 09, 2011 at 02:52 PM
can i insert the function OnMouseDown in the camera script? I want to remove the second button and then give every object his own script. Something like:
Object 1:
var cam1 : Camera;
var cam2 : Camera;
function Update () {
if(Input.GetKeyDown("1")){ >>>>Now here the function OnMouseDown () {
cam1.enabled = true;
cam2.enabled = false;
}
Object 2:
var cam1 : Camera;
var cam2 : Camera;
function Update () {
if(Input.GetKeyDown("1")){ >>>>Now here the function OnMouseDown () {
cam1.enabled = false;
cam2.enabled = true;
}
No, the On$$anonymous$$ouseDown goes on the objects themselves. What you do is put a function in script called SwitchCam on an empty object and cache that empty object in your camera scripts. I'll edit my post to show you.
P.S., please don't use answers to comment on someone else's answers... that's not really their purpose. Use the comments section...
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Wrong camera position/orientation on windows 10 store build: How to fix it? 0 Answers
Object not centering with screen.width/2 0 Answers
Setting camera to terrain generated billboards 0 Answers
Input.mousePosition not updating? 1 Answer