x


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);

}

more ▼

asked Sep 03 '10 at 08:04 AM

Louis 1 gravatar image

Louis 1
1 2 2 2

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!

Sep 03 '10 at 08:16 AM Marowi

ok. noted.. thanks for the help =)

Sep 09 '10 at 02:05 AM Louis 1
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);
   }
}
more ▼

answered Sep 03 '10 at 08:14 AM

Marowi gravatar image

Marowi
4.9k 4 14 53

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Oct 16 '10 at 06:27 AM

diabloroxx gravatar image

diabloroxx
689 5 7 12

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2998
x885
x786
x498
x225

asked: Sep 03 '10 at 08:04 AM

Seen: 5328 times

Last Updated: Sep 03 '10 at 08:04 AM