Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GUI button for camera rotation

Discussion in 'Immediate Mode GUI (IMGUI)' started by Noobzilla, Jan 11, 2010.

  1. Noobzilla

    Noobzilla

    Joined:
    Dec 31, 2009
    Posts:
    41
    Hi everyone - new to Unity and Javascript, so sorry in advance for my density. I'm sure I've seen the answer to my question several times, but I'm just not putting it together. I have a script for "rotate camera" which I pulled from the shadow demo. It works exactly the way I want, except that instead of using the keyboard arrow keys to rotate, I want to use buttons on a GUI. Here's the rotate camera script:

    Code (csharp):
    1.  
    2. var target : Transform;
    3. var edgeBorder = 0.1;
    4. var horizontalSpeed = 360.0;
    5. var verticalSpeed = 120.0;
    6. var minVertical = 20.0;
    7. var maxVertical = 85.0;
    8.  
    9. private var x = 0.0;
    10. private var y = 0.0;
    11. private var distance = 0.0;
    12.  
    13. function Start()
    14. {
    15.     x = transform.eulerAngles.y;
    16.     y = transform.eulerAngles.x;
    17.     distance = (transform.position - target.position).magnitude;
    18. }
    19.  
    20. function LateUpdate()
    21. {
    22.     var dt = Time.deltaTime;
    23.     x -= Input.GetAxis("Horizontal") * horizontalSpeed * dt;
    24.     y += Input.GetAxis("Vertical") * verticalSpeed * dt;
    25.    
    26.     y = ClampAngle(y, minVertical, maxVertical);
    27.      
    28.     var rotation = Quaternion.Euler(y, x, 0);
    29.     var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    30.    
    31.     transform.rotation = rotation;
    32.     transform.position = position;
    33. }
    34.  
    35. static function ClampAngle (angle : float, min : float, max : float) {
    36.     if (angle < -360)
    37.         angle += 360;
    38.     if (angle > 360)
    39.         angle -= 360;
    40.     return Mathf.Clamp (angle, min, max);
    41. }
    42.  
    And, I've even made GUI buttons that work great for the zoom in/out function I want, that goes like this:

    Code (csharp):
    1.  
    2.     if(GUI.RepeatButton(Rect(Screen.width/2 -19 , Screen.height -50, 18, 18), "+")){
    3.     camera.main.fieldOfView = camera.main.fieldOfView - .5;
    4.     }
    5.    
    6.     if(GUI.RepeatButton(Rect(Screen.width/2 +1 , Screen.height -50, 18, 18), "-")){
    7.     camera.main.fieldOfView = camera.main.fieldOfView +.5;
    8.     }
    9.  
    So, now it seems all I have to do is combine them somehow to use GUI keys to do what the keyboard arrow buttons are doing in the rotate script, but I can't figure out how to make it work. Thanks for any hints you can provide - I will take no offense if you need to talk down to me with details ("Type this HERE and attatch that HERE"), I'm sure I've seen the answer in here somewhere, but things like "you just need to set up a variable and pass it to the function" tend to mystify me more. Thanks again.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The main difference is that you need to put all GUI code (or at least the calls to GUI.xxx functions) in the OnGUI function:-
    Code (csharp):
    1. function OnGUI() {
    2.     if (GUI.RepeatButton(...)) {
    3.         // Camera code
    4.     }
    5. }
    The only other slight difference is that Input.GetAxis returns a value from -1 to +1 depending on which direction is input. Obviously, you need to do this explicitly with your buttons, so you'll have something like:-
    Code (csharp):
    1.  
    2. if (GUI.RepeatButton(...)) {    // Left button.
    3.     x -= horizontalSpeed * dt;
    4. }  else if (GUI.RepeatButton(...)) {    // Right button.
    5.     x += horizontalSpeed * dt;
    6. }
     
    deepportion likes this.
  3. Noobzilla

    Noobzilla

    Joined:
    Dec 31, 2009
    Posts:
    41
    Thanks so much Andeeee -- this helps a lot. Appreciate your time!

    NZ
     
  4. Grave_sts

    Grave_sts

    Joined:
    Jan 9, 2012
    Posts:
    3
    Hey NoobZilla~

    I am trying to achieve something similar to this, I was wondering if I could possible see your completed script or scene? I am quite noob with scripting but am able to understand the outline ;)

    Thanks in advance
     
  5. deepportion

    deepportion

    Joined:
    Jul 7, 2017
    Posts:
    6

    in love with your brain "Andeeee"