Search Unity

[SOLVED] GUI button movement

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

  1. hamsterhill

    hamsterhill

    Joined:
    Jul 12, 2009
    Posts:
    17
    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;
    14.  
    15.  
    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);
    22.  
    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.     }
    68.  
    69.  
    70.  
    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

    Eric5h5

    Volunteer Moderator Moderator

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

    --Eric
     
  3. hamsterhill

    hamsterhill

    Joined:
    Jul 12, 2009
    Posts:
    17
    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

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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.

    --Eric
     
  5. hamsterhill

    hamsterhill

    Joined:
    Jul 12, 2009
    Posts:
    17
    :D legend thanks alot man