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.
  2. Dismiss Notice

how to move the literal button when clicked

Discussion in 'Scripting' started by dcruz26, Aug 5, 2016.

  1. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    Im currently making a word game so my button when clicked would go upwards to the blank and when it's clicked again it will go back to it's original position (not clicked)
    ( upload_2016-8-5_21-39-33.png
    (clicked)
    upload_2016-8-5_21-40-31.png
     
  2. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    and i dont know how to move the button :)
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Since they are buttons, use a tweening engine such as LeanTween or Dotween(both are free on the asset store). I use LeanTween myself and they have a variety of simple calls you can do to animate to positions. They seem especially well suited for moving UI components (but can do other stuff).

    https://www.assetstore.unity3d.com/en/#!/content/3595
    https://www.assetstore.unity3d.com/en/#!/content/27676

    So basically with one of these, you'll click the button, run the tween on it to move it up, or click and move it down (some check to determine which direction to go).
     
  4. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    To move a button can't you use something like Transform.translate?
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Of course you can use standard unity stuff. But tweening engines make it super simple and good tweening engines don't really add much to the project. The benefits are worth it. Especially since you can do something like have a piece move and then when it's finished moving you can call a method oncomplete to run code, or you can do an onupdate call to do something while it's moving.
     
  6. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Oh nice! I've never used tweening engines so I'll have to check them out myself lol
     
  7. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    Thank you very much but what if i use transform.Translate tho how should i do it
    Code (CSharp):
    1. ublic class MOve : MonoBehaviour {
    2.      private Button button1;
    3.     // Use this for initialization
    4.     void Start () {
    5.         button1 = GetComponent<Button>();
    6.     }
    7.    
    8.     // Update is called once per frame
    9.     void Update () {
    10.      
    11.          
    12.         }
    13.     void moveup()
    14.     {
    15.         if (Input.GetMouseButtonDown(0))
    16.         { button1.transform.Translate(131, 22, 0); }
    17.     }
    18.  
    19.     }
    20.  
    21. this is my code
    upload_2016-8-6_14-40-37.png how do i do it?
    im just asking so i know how to do it in both ways
    and do you know any tutorials on DOTWEEN?
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    transform.translate requires you to basically have it move in a direction. To get it to go to a particular spot, you'll have to do some math and figure out what direction you need to go and how to get it there, plus doing checks for when it gets there so it stops moving. Honestly, for this situation, it's more trouble to use then it's worth.

    The reason I suggest a tween is you don't have to do that. You set a start position, an end position, and the time you want it to take to get there. Plus, once it's there, you can do a complete call to execute some code if you want it to do something else.

    Chances are, what you really want if you don't plan to use a tweening engine is to use Lerp. Vector3.Lerp allows you to declare a start and end position and lets you declare a number from 0-1 to determine where in the path it is. So at .5 it would be halfway to the end position. You'll have to increment this number gradually depending on how fast you want something to move. (The tween engine will handle this for you, you just tell it how quickly you want the object to move. You can also add cool effects like bounce and such if you wanted)

    As far as Dotween, I've never used it. They seem to have some examples on their site http://dotween.demigiant.com/examples.php, plus I'm sure there are some youtube videos on it as well. I use LeanTween currently as it was the one we use at work and it's one of the better ones. Dotween is also really good I've heard, but I stuck with the tween that I had already been using at work.
     
  9. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    can i use leantween or dotween in 2d?
     
  10. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    and how to i make it move when i click it?
     
  11. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    To find tutorials, there are amazing tools out there.
    My favorites:
    Bing.com
    Google.com
    YouTube.com

    Just in less than a minute I found this:


    YouTube is a great place for Unity3D tutorials.

    As for using Transform.Translate, you'll need to look here in the (mostly self-explanatory) docs: http://docs.unity3d.com/ScriptReference/Transform.Translate.html
     
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    leantween and dotween both work on 2d and 3d. It's not going to fit every scenario (like where you want your player to control the movement of a char), but if you are doing movements, color changes, scaling, etc, they are pretty amazing.