- Home /
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?
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.
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.
@Unitard That sounds like an answer. Please convert your comment to an answer so you can get more credit for it!
Answer by You! · May 25, 2012 at 04:45 AM
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).
}
}
Answer by pborg · May 25, 2012 at 05:25 AM
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?
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
Answer by pborg · May 25, 2012 at 05:34 AM
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?
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.
I couldn't see the comment I made, so I double posted. sorry