Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

[SOLVED]OnGUI Buttons React Differently Than Key Press

Discussion in 'Immediate Mode GUI (IMGUI)' started by Sp33dy_Sn1pa, Oct 10, 2014.

  1. Sp33dy_Sn1pa

    Sp33dy_Sn1pa

    Joined:
    Sep 27, 2014
    Posts:
    15
    So I think this might be the most trouble I've had with anything in unity thus far. I have inserted a couple of OnGUI buttons into my character controller script. The script was created to force the 2D player to move from left to right at the speed and rotation speed i set in the inspector with only 2 key presses, up or down arrow. In turn, the players look rotation would either go up or go down and it would maintain the speed I set which works perfectly on keyboard press. The GUI buttons I put in only react on touch screen after my finger has left the GUI button. What I would like to have happen is, when the GUI button is pressed, then the controls react as long as the button is held down all in the same script. Can anyone help me with this please? Any advice would be greatly appreciated.

    Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class SnakeHead : MonoBehaviour
    6. {
    7.     public float fTurnRate = 200; // Degrees of turning per second
    8.     public float fSpeed = 9.5f; // Units per second of movement
    9.     public Tail var;  
    10.     public bool dead = false;
    11.    
    12.     public bool debugMode = false;
    13.     public bool centerButton = false;
    14.     public Texture2D buttonImage = null;
    15.     public Texture2D otherImage = null;
    16.     private bool invertClick = false;
    17.     private Texture2D currentImage = null;
    18.     public string buttonLabel = "Lable Name Here";
    19.     public string otherbuttonLabel = "Lable Name Here";
    20.  
    21.     public Rect position = new Rect(15, 15, 150, 150);
    22.     public Rect positionTwo = new Rect(15, 15, 150, 150);
    23.     public Rect otherposition = new Rect(15, 300, 150, 150);
    24.     public Rect otherpositionTwo = new Rect(15, 300, 150, 150);
    25.     private Rect currentRect = new Rect(15, 15, 150, 150);
    26.     private Rect otherRect = new Rect(15, 300, 150, 150);
    27.  
    28.     public string title = string.Empty;
    29.     public GUISkin skin = null;
    30.    
    31.    
    32.    
    33.     private void OnGUI()
    34.     {
    35.         GUI.skin = skin;
    36.        
    37.         if (debugMode || Application.isPlaying)
    38.         {
    39.             if (centerButton)
    40.             {
    41.                 currentRect.x = (Screen.width * 0.5f) - (currentRect.width * 0.5f);
    42.                 currentRect.y = (Screen.height * 0.5f) - (currentRect.height * 0.5f);
    43.                 otherRect.x = (Screen.width * 0.5f) - (otherRect.width * 0.5f);
    44.                 otherRect.y = (Screen.height * 0.5f) - (otherRect.height * 0.5f);
    45.             }
    46.            
    47.             if (GUI.Button(currentRect, buttonLabel))
    48.             {
    49.                 transform.Rotate (Vector3.forward * fTurnRate * Time.deltaTime);
    50.                 invertClick = !invertClick;
    51.                
    52.                 if (invertClick)
    53.                 {
    54.                     currentImage = otherImage;
    55.                     currentRect = positionTwo;
    56.                 }
    57.                 else
    58.                 {
    59.                     currentImage = buttonImage;
    60.                     currentRect = position;
    61.                 }
    62.             }
    63.             if (GUI.Button(otherRect, otherbuttonLabel))
    64.             {
    65.                 transform.Rotate (-Vector3.forward * fTurnRate * Time.deltaTime);
    66.                 invertClick = !invertClick;
    67.                
    68.                 if (invertClick)
    69.                 {
    70.                     currentImage = otherImage;
    71.                     otherRect = otherpositionTwo;
    72.                 }
    73.                 else
    74.                 {
    75.                     currentImage = buttonImage;
    76.                     otherRect = otherposition;
    77.                 }
    78.             }
    79.         }
    80.     }
    81.    
    82.     void Update ()
    83.     {
    84.         if (Input.GetKey (KeyCode.UpArrow))
    85.         transform.Rotate (Vector3.forward * fTurnRate * Time.deltaTime);
    86.        
    87.         if (Input.GetKey (KeyCode.DownArrow))
    88.         transform.Rotate (-Vector3.forward * fTurnRate * Time.deltaTime);
    89.        
    90.         transform.localPosition = transform.localPosition + transform.right * fSpeed * Time.deltaTime;
    91.        
    92.         var.TailUpdate();
    93.     }
    94. }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,355
    You can use GUI.RepeatButton, however it's not a good idea to put movement code in OnGUI. Use RepeatButton to toggle boolean variables, which you can then use in Update.

    --Eric
     
    Sp33dy_Sn1pa likes this.
  3. Sp33dy_Sn1pa

    Sp33dy_Sn1pa

    Joined:
    Sep 27, 2014
    Posts:
    15
    Ok, so here is what I fixed using your recommendations.

    Code (CSharp):
    1. public bool FlyUp = false;
    2. public bool FlyDown = false;
    3.  
    4. public void OnGUI()
    5. {
    6. if (GUI.RepeatButton(currentRect, buttonLabel))
    7.             {
    8.                     FlyUp = true;
    9. }
    10. if (GUI.RepeatButton(otherRect, otherbuttonLabel))
    11.             {
    12.                 FlyDown = true;
    13. }
    14. void Update()
    15.     {
    16.  
    17.         if (FlyUp)
    18.         transform.Rotate (Vector3.forward * fTurnRate * Time.deltaTime);
    19.      
    20.         if (FlyDown)
    21.         transform.Rotate (-Vector3.forward * fTurnRate * Time.deltaTime);
    22.      
    23.         transform.localPosition = transform.localPosition + transform.right * fSpeed * Time.deltaTime;
    24.      
    25.         var.TailUpdate();
    26.     }
    27. }
    Now it works to a degree. When the button is pressed, it reacts exactly how i want it however, when the button is released or the other button is pressed, it maintains the path of the first button pressed. Its like once i hit a button, it stays held down even if I release my finger from it. Suggestions?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,355
    Turn flyUp/flyDown off when the button isn't pressed.

    --Eric
     
  5. Sp33dy_Sn1pa

    Sp33dy_Sn1pa

    Joined:
    Sep 27, 2014
    Posts:
    15
    That's what I figured. I'm still pretty new at this so I'm currently scouring the interweb to learn how to do just that lol. Thank you for pointing me in the right direction though.