- Home /
How do I move the camera to another object in the scene on mouse down?
I want to attach a script to my object that when I click on that object, the camera moves to another object.
So, currently my main camera is focused on one object but I want that camera to move when I click on a specific object and land on another object.
I was thinking since i have the coordinates of both objects, I can move the camera on mouse down from coordinate (x1, y1, z1) to (x2, y2, z2) but I'm not sure how or what function to use.
Answer by Jinxology · Jan 06, 2014 at 09:49 PM
Add a script to your clickable objects that says:
var mainCam:Camera;
function OnMouseUp() {
mainCam.transform.position = new Vector3(transform.position.x, transform.position.y, mainCam.transform.position.z);
}
Make sure to drag your main camera to the mainCam field in the inspector. I left the Z position on the camera the same since I figured your may want to keep the distance away from the object the same, and just adjust the X/Y.
I'm getting the error: Error CS1519: Invalid token ';' in class, struct, or interface member declaration (CS1519) (Assembly-CSharp-firstpass)
Ahh, you are using C#, but my code example is in Javascript.
I rewrote in C#:
public Camera mainCam;
void On$$anonymous$$ouseUp() {
mainCam.transform.position = new Vector3(transform.position.x, transform.position.y, mainCam.transform.position.z);
}
However, I realized I need to add more detail. $$anonymous$$y main camera is looking at several objects right now. When I click one object, it zooms into the object and deactivates the other objects. However, there's another object on that zoomed in object, so that when I click on that object, I want the camera from that view to move to the new object (x,y) distance away. Right now, it's only moved a little bit from where the zoomed in object is and not toward the other object.
So, basically, I want to set wherever the camera is currently to the other object far, far away
Actually, the problem is that the camera is too zoomed in when I put the coordinates of where the camera should be like manually entered in the vector3 above. hmm