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

Resolved Problems with Lerp in UI

Discussion in 'UGUI & TextMesh Pro' started by Alvarden, Mar 26, 2019.

  1. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    60
    Hello, everyone. This is my first post in here (really) and i'm in need of some help. I'm new in Unity and i wanted to make a button to move with Lerp from one position to another (im my case, to the center of the screen) everytime i click on it. The problem is that it moves too fast: it just moves instantly from one place to another.
    Here's my code so far:

    Code (CSharp):
    1.   Vector3 originalPosition;
    2.     Vector3 destino;
    3.     GameObject[] buttons;
    4.     Animation[] anims;
    5.     float count;
    6.     float distancia;
    7.     private float startTime;
    8.     float speed=1.0f;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.  
    15.         anims = new Animation[7];
    16.      
    17.         originalPosition = new Vector3(gameObject.transform.position.x,
    18.             gameObject.transform.position.y,gameObject.transform.position.z);
    19.  
    20.         buttons = GameObject.FindGameObjectsWithTag("buttons");
    21.         destino = new Vector3(Screen.width / 2, Screen.height / 2, 0);
    22.        
    23.         for (int i=0;i<buttons.Length;i++)
    24.         {
    25.            
    26.             anims[i] = buttons[i].GetComponent<Animation>();
    27.          
    28.         }
    29.  
    30.        
    31.         count = 2;
    32.        
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.        
    39.     }
    40.  
    41.  
    42.     public void cambiar()
    43.     {
    44.         startTime = Time.time;
    45.         distancia = Vector3.Distance(originalPosition, destino);
    46.         float dis=(Time.time-startTime)*speed;
    47.         float viaje=dis/distancia;
    48.         if (count==1)
    49.         {
    50.             transform.position = Vector3.Lerp(originalPosition, destino, viaje);
    51.  
    52.  
    53.             foreach (Animation an in anims)
    54.             {
    55.                
    56.                 if (an.gameObject!=gameObject)
    57.                 {
    58.                     an.Play("appearButton");
    59.                    
    60.                 }
    61.                
    62.              
    63.             }
    64.             count++;
    65.         }
    66.         else
    67.         {
    68.            
    69.             transform.position = Vector3.Lerp(destino, originalPosition, viaje);
    70.  
    71.            
    72.             foreach (Animation an in anims)
    73.             {
    74.                
    75.                 if (an.gameObject != gameObject)
    76.                 {
    77.                     an.Play("disappearButton");
    78.                 }
    79.  
    80.             }
    81.            
    82.             count = 1;
    83.         }
    84.        
    85.     }
    I tried some alternatives i saw in other forums, but so far, no success. Please, let me know the mistake i made.
    Thank you for your pacience.
     
  2. Avalin

    Avalin

    Joined:
    Oct 12, 2018
    Posts:
    98
    Have you tried adjusting the speed variable? I would assume that the higher the number, the slower it'll be. Alternatively try fiddling with your "viaje" variable in the lerp function and see what changes
     
  3. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    60
    I did those changes, unfortunately there was no effect.
     
  4. NorthStar79

    NorthStar79

    Joined:
    May 8, 2015
    Posts:
    88
  5. CG522

    CG522

    Joined:
    Dec 6, 2016
    Posts:
    3
    @Alvarden
    The main problem I see is that you are not using the Update () function.
    When lerping, you move a small amount every frame, that's how you get the animation.
    The function that you have now, with no Update function, will move to wherever you said in one frame.
    https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/
    Read this article a couple times. Lerping can be hard to wrap your head around in the beginning.
    Cheers!
     
    Perturbator858 likes this.
  6. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    60
    I tried with that, but for some reason, everytime i click on the button, it disappears. Not sure if there is a solution for that.
     
    Last edited: Apr 1, 2019
  7. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    60
    Thing is, the script has to be called everytime i click on the button, hence why it's not on the update function. That was on purpose, but now i'm trying to make it work properly. Or it's seriously not possible without the update function?
     
  8. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    60
    Yeah, sorry for the delay. I was really busy and i was testing the solutions you told me so far.
    By the way, i forgot to mention: the application i'm creating is an AR app, so if there is any trick you know about ui with AR, please let me know.
     
    Last edited: Apr 1, 2019
  9. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    60
    Well, nevermind. Let's leave it like that.
     
  10. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    60
    All right, it's been a while since then and i want to just write in here for future reference and for those having a similar problem and ends up here:
    I suggest trying the asset DOTween to achieve UI Animations with ease with a few lines of code instead of trying what i showed on the first post. I'll leave the asset link and its documentations if you wanna try it. Don't worry, it's easier than it seems.

    https://assetstore.unity.com/packages/tools/animation/dotween-hotween-v2-27676
    http://dotween.demigiant.com/documentation.php

    And also i would advice using Coroutines if you wanna create functions o methods that behaves like it was on update, but you only need them once (not sure if that clarifies anything, let me know if not). It's gonna be very useful in the future.


    https://docs.unity3d.com/Manual/Coroutines.html
     
    Last edited: Aug 25, 2020
  11. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    60
    All right, i'll leave this thread for now. Good luck everyone on your projects and i hope this tip was useful.