1. Some Unity services have been affected by the DDoS of large DNS provider today. Click here for more information.
  2. Become a closed-alpha partner on the new Facebook games platform. Read more about it here.
  3. Enter the Samsung TIZEN App Challenge for a chance to win prizes. Read more about it here.
  4. The 5.5 Beta is now available. Click here for more details!
  5. Get early access to our Google Daydream Technical preview. Click here for more details!

[SOLVED] GUI button movement

Discussion in 'Scripting' started by hamsterhill, Nov 1, 2009.

  1. hamsterhill


    Jul 12, 2009
    hi all,
    ok im pretty new to unity and im makeing a simple web app which basicly lets you look at a 3d model in the browser... the model is of a large city scape so the camera needs to be able to pan and rotate around it in all directions... i have managed to get it to rotate and zoom the way i want it too but i have 2 problems

    1. The way i do it at the moment is like this
    Code (csharp):
    1. var cam : Transform;
    2. var pivot : Transform;
    3. var controll: Transform;
    4. var image_rotate : Texture2D;
    5. var image_pan : Texture2D;
    6. var image_zoom : Texture2D;
    7. var image_reset : Texture2D;
    8. var customButton : GUIStyle;
    9. private var zoom = 0 ;
    10. private var rotx = 45 ;
    11. private var panx = 0 ;
    12. private var panz = 0 ;
    13. private var moveDirection = Vector3.zero;
    16. function OnGUI () {
    17.     // Make a background's
    18.     GUI.Box (Rect (545,6,49,58), image_rotate,customButton);
    19.     GUI.Box (Rect (545,70,49,58), image_pan,customButton);
    20.     GUI.Box (Rect (545,134,49,58), image_zoom,customButton);
    21.     GUI.Box (Rect (545,198,49,58), image_reset,customButton);
    23. //make buttons
    24. //rotate buttons
    25.     //up
    26.     if (GUI.Button (Rect (560,3,18,18), "")) {
    27.         print ("rotate up pressed");
    28.         if (rotx<80){
    29.         pivot.Rotate(5,0,0);
    30.         rotx+=5;
    31.         }else {print ("rotate maxed");}
    32.     }
    33.     //down
    34.     if (GUI.Button (Rect (560,39,18,18), "")) {
    35.         print ("rotate down pressed");
    36.         if (rotx>15){
    37.         pivot.Rotate(-5,0,0);
    38.         rotx-=5;
    39.         }else {print ("rotate mined");}
    40.     }
    41.     //left
    42.     if (GUI.Button (Rect (542,21,18,18), "")) {
    43.         print ("rotate left pressed");
    44.         controll.Rotate(0,5,0);
    45.     }
    46.     //right
    47.     if (GUI.Button (Rect (578,21,18,18), "")) {
    48.         print ("rotate right pressed");
    49.         controll.Rotate(0,-5,0);
    50.     }
    51. //zoom buttons
    52.     //up
    53.     if (GUI.Button (Rect (550,10+128,40,20), "")) {
    54.         print ("zoom up pressed");
    55.             if (zoom<500){
    56.         cam.Translate(0,0,20);
    57.         zoom+=20;
    58.         }else {print ("zoom mined");}
    59.     }
    60.     //down
    61.     if (GUI.Button (Rect (550,30+128,40,20), "")) {
    62.         print ("zoom down pressed");
    63.         if (zoom>-100){
    64.         cam.Translate(0,0,-20);
    65.         zoom-=20;
    66.         }else {print ("zoom maxed");}
    67.     }
    71. }
    the problem is that the code is only exicuted when i relese the button... is there a way to do it so that the code is exicuted the entire time the button is being pressed?

    2. Ok the other problem I'm having is with "panning" , the way i have set my objects up means that the "controll" object's x and z are always horizontal and facing the right way... so translation should be easy but i want to put a limit on it. the way i currently move is just using the FPS walker and then i have an object that i use an invisible wall around the limits. but i need a way to ether convert the pressing of the GUI button to the way the charactercontroller.move works or some other way to limit the movement...

    thanks for reading and any help

    p.s. this is one of my first unity projects and first time I'm using javascript, up until now I've been slashing together stuff from the standard assets and the help files so let me know if there is a better way to do this.

    cheers HamsterHill
  2. Eric5h5


    Volunteer Moderator Moderator

    Jul 19, 2006
    1. Gui.RepeatButton
    2. Not sure I quite understand the question, but Mathf.Clamp is a good way to limit numbers to certain ranges.

  3. hamsterhill


    Jul 12, 2009
    wow, thanks for the quick reply... that solved my first problem.

    what i mean for the secound one is basicly i want to basicly use the FPS walk script but i want to change the input from the WASD too gui buttons, how do i do that?

    cheers hamster
  4. Eric5h5


    Volunteer Moderator Moderator

    Jul 19, 2006
    You could change that script so instead of using "Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));" you could use variables like "Vector3(horizontal, 0, vertical);" and then set the horizontal and vertical variables depending on the state of the appropriate GUI buttons.

  5. hamsterhill


    Jul 12, 2009
    :D legend thanks alot man