Search Unity

Moving panel out and in, then it pops out again

Discussion in 'UGUI & TextMesh Pro' started by pKallv, Sep 4, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I have a small panel with three buttons:

    1.) invisible that is places on the edge of the panel so it is clickable, that moves out the panel to the scene
    2.) a cancel button that moves the panel back out of the scene
    3.) a submit button, with no function at the moment



    Currently i have the same script attached to both the Panel- and the Cancel-button. Earlier i had two separate scripts attached to the buttons and got the same result.

    The problem i have is that when i click on the PanelButton the panel moves out and when i click on the cancel button the panel moves back in. However, here is were the problem starts as when the panel is in it immediately moves out again. I have been testing, testing, testing and scratching my head over this... and would appreciate some advice.

    Here is the code i use:
    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingUnityEngine.UI;
    4. usingSystem.Collections;
    5.  
    6. public class MovePanelScript : MonoBehaviour {
    7.  
    8.    public GameObject thePanel;
    9.    public GameObject theButton;
    10.    private bool moveMenuPanelOut = false;
    11.    private bool moveMenuPanelIn = false;
    12.    private bool isMenuPanelInCorrectPlace = false;
    13.    public float panelSpeed = 2.0f;
    14.    private bool moveOut = false;
    15.    private bool moveIn = false;
    16.  
    17.    public void MoveThePanel() {
    18.       if (theButton.tag == "GameBoardPanelButton") {
    19.       moveMenuPanelOut = true;
    20.       moveOut = true;
    21.       moveIn = false;
    22.    }
    23.    else if (theButton.tag == "GameBoardCancelButton") {
    24.       moveMenuPanelIn = true;
    25.       moveOut = false;
    26.       moveIn = true;
    27.    }
    28. }
    29.  
    30. void Update() {
    31.    if ( moveOut ) {
    32.       //Move the panel into the scene
    33.       if ( moveMenuPanelOut ) {
    34.          Vector3 menuPanelPos = thePanel.GetComponent<RectTransform>().localPosition;
    35.          thePanel.GetComponent<RectTransform>().localPosition = Vector3.Lerp(thePanel.GetComponent<RectTransform>().localPosition, newVector2(260.0f, 0.0f), panelSpeed * Time.deltaTime);
    36.  
    37.          if (menuPanelPos.x < 240.0f) { //This is to save cpu-cycles and stop all processing
    38.             moveMenuPanelOut = false;
    39.             moveOut = false;      
    40.          }
    41.       }
    42.    }
    43.    else if ( moveIn ) {
    44.       //Move the panel back outside the scene
    45.       if ( moveMenuPanelIn ) {
    46.          Vector3menuPanelPos2 = thePanel.GetComponent<RectTransform>().localPosition;
    47.          thePanel.GetComponent<RectTransform>().localPosition =    Vector3.Lerp(thePanel.GetComponent<RectTransform>().localPosition, newVector2(472.0f, 0.0f), panelSpeed * Time.deltaTime);
    48.  
    49.          if (menuPanelPos2.x > 362.0f) { //Thisistosavecpu-cyclesandstopallprocessing
    50.             moveMenuPanelIn = false;
    51.             moveIn = false;
    52.           }
    53.        }
    54.     }
    55.  }
    56. }
    57.  
     
  2. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    Reading another person's code is always a bit of a challenge, especially without actually having the project in front of you, so apologies if I'm missing something. =)

    Code (csharp):
    1. thePanel.GetComponent<RectTransform>().localPosition = Vector3.Lerp(thePanel.GetComponent<RectTransform>().localPosition, new Vector2(472.0f, 0.0f), panelSpeed * Time.deltaTime);
    Here you are interpolating between two values by panelSpeed * Time.deltaTime. However, your code doesn't show you updating panelSpeed anywhere, which would suggest it shouldn't even be moving in the first place! Typically you wouldn't use a `speed` variable here, but rather something like `progress` that you're incrementing from 0 to 1 over time (speed / distance eg). You're also trying to interpolate between a Vector3 and a Vector2, which doesn't seem right to me.

    That said, I would like to recommend the iTween library to you for this. It's free to use and provides a really nice, clean interface that lets you get rid of a lot of variables and clutter. Then your code could be as simple as

    Code (csharp):
    1. iTween.MoveTo(thePanel, iTween.Hash("from", thePanel.transform.localPosition, "to", new Vector3(472f, 0f, 0f, "speed", panelSpeed)))
    Now with all that said, once you start anchoring your UI elements you'll probably want to refrain from moving the position of elements directly, and instead animate their `offsetMin` and `offsetMax` values (using iTween you can do this with iTween.ValueTo() and a simple positioning callback).

    Hope this helps a bit!
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    Senshi, appreciate you answer and will try out your suggestions.