Search Unity

Trying to move a script based GUI with lerp

Discussion in 'Scripting' started by InSights, Mar 25, 2013.

  1. InSights

    InSights

    Joined:
    Feb 1, 2013
    Posts:
    3
    Hi all

    As the title suggests I've created a ‘GUI Texture’ through scripting and I’m trying to move this element from left to right. It currently sits just off the left side of the screen and I want it to move to the right, visible to the user when he/she presses the TAB key. The GUI is built using Unity’s default system. At the moment I'm not bothering with the TAB, I just want to get 1 thing working :p

    Here’s my code so far. I got it to work when implementing a standard GUITexture, but I’d prefer to use this method as I’ll have far less objects in my Hierarchy panel. I've also attached some screenshots to help explain.

    Thanks in advance

    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4.  
    5. var beep : AudioClip;
    6. var menuSkin : GUISkin;
    7.  
    8. var nameField : Rect;
    9. var ageField : Rect;
    10. var sexField : Rect;
    11.  
    12. var nameBMB : String;
    13.  
    14. var xStartPosition : float = -1.0;
    15. var xEndPosition : float = 0.5;
    16. var speed : float = 1.0;
    17. private var startTime : float;
    18.  
    19. private var menuArea : Rect;
    20.  
    21. function Start () {
    22.    
    23.     menuArea = Rect(xStartPosition, Screen.height / 4, 384.0, 412.0);
    24. }
    25.  
    26. function Update () {
    27.  
    28.     transform.position.x = Mathf.Lerp(xStartPosition, xEndPosition, Time.deltaTime * speed);
    29.    
    30.     if(Input.GetKeyDown(KeyCode.Tab)){
    31.         Debug.Log("Show the stats menu");
    32.     }
    33.  
    34.     if(Input.GetKeyUp(KeyCode.Tab)){
    35.         Debug.Log("Hide the stats menu");
    36.     }
    37.    
    38. }
    39.  
    40. function OnGUI(){
    41.  
    42.     GUI.skin = menuSkin;
    43.     GUI.BeginGroup(menuArea);
    44.    
    45.     GUI.Box(Rect(0.0, 0.0, 384.0, 412.0), "Stats Menu");
    46.    
    47.     GUI.Label(Rect(nameField), "Name: " + nameBMB);
    48.     GUI.Label(Rect(ageField), "Age: ");
    49.     GUI.Label(Rect(sexField), "Sex: ");
    50.    
    51.     GUI.EndGroup();
    52. }
    53.  
    54.  
    55. @script RequireComponent(AudioSource)
    56.  
    57.  

    $Screenshot1.png $Screenshot2.png
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You would lerp the x part of the menuArea rect, in the same way that you're lerping transform.position.x. (Although it would be better to use a coroutine instead of Update; see here for an example of that.)

    --Eric
     
    Major0 likes this.
  3. InSights

    InSights

    Joined:
    Feb 1, 2013
    Posts:
    3
    Hey, thanks for the reply.

    I got the lerp to work, using your suggestion but it only works in the start() function. I’m guessing that’s due to some variable constantly being reset when the update() function is called?

    As for the MoveObject suggestion, I’m not sure it’s going to work for what I need..unless there’s a bit of code I’m missing to link it to the UnityGUI elements. As from what I can tell the code is moving an object in 3D space, which in my project is currently an empty object with a script attached to it (StatsMenuScript). That’s not what I want to move, I want to move the UnityGUI elements that are being created by the script, when the user presses the TAB key.

    I’ve tried making a GUITexture object and then creating labels inside a script, but then the GUITexture moves and the labels don’t. So I’m going to stick with lerp at the moment as it’s giving me the closest result to what I want. I just need to figure out how to trigger it outside the start() function.

    *Edit* Is it less expensive to create this part of my GUI through scripting rather than having multiple GUITexture/ GUIText objects? It probably is, but it's worth asking... cause if it isn't, then I can just nest the GUIText objects under a GUITexture in the hierarchy panel and use your MoveObject script :)
     
    Last edited: Mar 26, 2013
  4. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    You obviously don't understand what Eric's code does. If you did, you could easily adjust the MoveObject subroutine to work on just variable x of your GUI Texture. Here, let me do that for you.

    Code (csharp):
    1.  
    2. var x : float;
    3.  
    4. function LerpVariable ( start : float, end : float, speed : float ) {
    5.  
    6.         var rate : float = 1.0f / (end - start) * speed;
    7.         var delta : float = 0;        
    8.         while( delta < 1.0f ) {
    9.      
    10.                delta += Time.deltaTime * rate;
    11.                x = Mathf.Lerp(start, end, delta);
    12.                yield;
    13.              
    14.        }
    15.  
    16. }
    17.  
    18. function Start (  ) {
    19.  
    20.           yield LerpVariable(-1.0f, 0.5f, 1.0f);
    21.  
    22. }
    23.  
    24. function OnGUI ( ) {
    25.         GUI.DrawTexture(Rect(x, Screen.height / 4, 384.0, 412.0), someTexture);
    26. }
    27.  
    Try to understand the workings of Lerp, it will help you apply it to many situations. It's not 3D space specific.
     
  5. InSights

    InSights

    Joined:
    Feb 1, 2013
    Posts:
    3
    Mmm you’re probably right there, my understand was different.. but I see what he meant now. The code you pasted works, but it does the same thing as when I got the lerp to work via Eric’s suggestion. I’ve tried using Event calls in the OnGUI() function and also a OnKeyDown() function to get the functionality when pressing down the TAB key. Yet it still doesn’t work. I’ve printed out the variables, and they’re changing but the actual graphical part doesn’t budge one bit :p Think I need to put in a little bit of research into this, unless you got any quick suggestions. Thanks