- Home /
button click to move camera to another position
hi, i want to move my camera when i click on a button. my button is a plane and has a OnMouseDown on it. However, i can't assign the gameobject as my camera. so im not sure even if my codes do work. here is my code. im new to unity so im not sure if it's right or wrong.
Thanks to anyone who can help and really sorry for the trouble.
var player : GameObject;
function OnMouseDown () { player.transform.Translate(1,1,1);
}
No need to apologise! One small request - when posting code, highlight it and press the icon with 0s and 1s on it, and it will format nicely. Not much of an issue here where the code is so compact, but with bigger examples it becomes difficult to read. Cheers!
Answer by · Sep 03, 2010 at 08:14 AM
OnMouseDown is only called when the user has pressed the mouse button while over the GUIElement or Collider. I think you want GetMouseButtonDown. You can also access the main camera via Camera.main.
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
Camera.main.transform.Translate(1,1,1);
}
}
Answer by diabloroxx · Oct 16, 2010 at 06:27 AM
Your code is correct. Just make sure that you add a collider to the Plane (which you want to assign as a button). You can also add a GUI button to translate or move your camera. That way your button will always be in view. I assume you will not want to move the Plane along with your camera.
Add collider by going to Physics and then collider.