x


How to make buttons move

I'm building a japanese study game and using buttons as the primary mode of movement between scenes. I'm working on a fade in. How do you make buttons move in from the sides?

more ▼

asked May 22 '12 at 06:36 AM

pborg gravatar image

pborg
21 1 2 10

Are they GameObjects or GUI-buttons?

May 22 '12 at 06:37 AM save

Store a Vector2 variable that interpolates from off-screen to in-screen in screenspace coordinates, in the Update() function. Then in the OnGUI() function, use that variable to position the button.

May 25 '12 at 04:24 AM Reavenk

All of my buttons are 3d objects parented to my camera, and all my UI text are text meshes. I move them around like I'd move around anything else. There must be some benefit to using the 2d GUI functionality- since most people do- but I'm not seeing it, and I seem to be avoiding a lot of the scaling and positioning problems that others have.

May 25 '12 at 03:02 PM Kiloblargh

@Unitard That sounds like an answer. Please convert your comment to an answer so you can get more credit for it!

May 25 '12 at 03:04 PM You!
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Make a variable which changes as you want the button to move, and have the button appear off-screen. Here is an example (in Javascript).

var buttonx : int = -100
var buttony : int = 200
// [...]
function OnGUI ()
{
if(GUI.Button(Rect(buttonx,buttony,100,200),"Some text is here..."))
{
//Whatever the button does is here.
}
}

The coding for however you want the button to move is elsewhere in the script. This is done by changing the values of buttonx and buttony in any way you want until you reach the button's destination.

To change the position of the button, continually change the value of buttonx and buttony in the Update() function, like you mentioned below. Here is an example:

function Update()
{
if (buttonx < 100)
{
buttonx += 5 //Move the button 5 pixels per frame to the right until it reaches its destination (100).
}
if (button y < 300)
{
buttony += 5 //Move the button 5 pixels per frame downward until it reaches its destination (300).
}
}
more ▼

answered May 25 '12 at 04:45 AM

You! gravatar image

You!
447 8 14 16

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

Would you then change the buttonx and buttony values in the update function? And how will it make a smooth transition instead of simply moving to the new location?

more ▼

answered May 25 '12 at 05:25 AM

pborg gravatar image

pborg
21 1 2 10

Please use the comment button for comments. If it does not show up, navigate back to your user profile and click on your question(this should reset the buttons). Thanks

May 25 '12 at 05:28 AM hijinxbassist
(comments are locked)
10|3000 characters needed characters left

Would you then change the buttonx and buttony values in the update() function? How do you move it smoothly rather than simply jumping to the new coordinates?

more ▼

answered May 25 '12 at 05:34 AM

pborg gravatar image

pborg
21 1 2 10

I updated my answer to the question. Also, please post comments using the "add new comment" button under an answer or a question, as @hijinxbassist suggested. It makes the question post clearer when it comes to others finding answers.

May 25 '12 at 02:51 PM You!

I couldn't see the comment I made, so I double posted. sorry

May 25 '12 at 06:56 PM pborg
(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:

x3768
x108

asked: May 22 '12 at 06:36 AM

Seen: 479 times

Last Updated: May 25 '12 at 06:56 PM