Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to Lerp UI object | Rect Transform

Discussion in 'Scripting' started by Only4gamers, Dec 13, 2020.

  1. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Hello everyone,
    I want to create this simple Moving UI object using script.
    I created this using Animation but there is some problem. So I want to create this using script. What I want to do is on Button click, increase/decrease Pos X of object by 1920. And I want to increase/decrease Pos X value instead of entering a fixed value. On Right click decrease Pos X by 1920 and on left click increase it by 1920. And object should animate like below example video.

    This is example, I created using animation:

    Capture.PNG
    Thanks.
     
    Last edited: Dec 13, 2020
  2. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    I succeed to complete my code, it's just not animating. Because I don't understand how to use Lerp here.
    Here is script:
    Code (CSharp):
    1.  
    2. RectTransform rectTransform;
    3. public GameObject next;  // Next Button
    4. public GameObject previous; // Previous Button
    5.  
    6. void Start()
    7. {
    8.    rectTransform = GetComponent<RectTransform>();
    9. }
    10.  
    11. public void OnClickNext()
    12.     {
    13.         rectTransform.anchoredPosition3D = new Vector3
    14.         (rectTransform.anchoredPosition3D.x-1920, rectTransform.anchoredPosition3D.y, 0);
    15.  
    16.         if(rectTransform.anchoredPosition3D.x == -3840)
    17.         {
    18.             next.SetActive(false);
    19.         }
    20.         else
    21.         {
    22.             previous.SetActive(true);
    23.         }
    24.     }
    25.  
    26. public void OnClickPrev()
    27.     {
    28.         rectTransform.anchoredPosition3D = new Vector3
    29.         (rectTransform.anchoredPosition3D.x+1920, rectTransform.anchoredPosition3D.y, 0);
    30.  
    31.         if(rectTransform.anchoredPosition3D.x == 0)
    32.         {
    33.             previous.SetActive(false);
    34.         }
    35.         else
    36.         {
    37.             next.SetActive(true);
    38.         }
    39. }
    40.  
    41.  
    Is anyone know how to use Lerp to compete my Script?
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Only4gamers

    "I succeed to complete my code, it's just not animating."

    Could it be because you don't seem to have any animation code? Did you paste all of your code?

    "Because I don't understand how to use Lerp here."

    I don't see any lerp code here either.

    There are several examples if you google, also, documentation has examples too:
    https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

    Also, I've never used rectTransform.anchoredPosition3D, I simply use position or localPosition. There is usually no reason to use anchoredPosition for RectTransform movement... anchors can be either split or together and IMHO anchor is not a good reference for RT position.
     
    Only4gamers likes this.
  4. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Thank you so much for your reply. Actually I am not talking about animation. I want to create animation like feel with Lerp. As I just learned about Lerp yesterday, I don't know how to use it in my code, that's why I am asking for help. I already read some Lerp tutorials but I didn't understand how to use Lerp in this case. After some research I find anchoredPosition3D working best for me, but If I can get same result with different code then I have no problem either.
     
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    "I am not talking about animation."

    By "animation code" I meant code that animates your object position.

    "As I just learned about Lerp yesterday, I don't know how to use it in my code, that's why I am asking for help."

    But did you actually read the documentation page link? There you can find several examples.

    "I already read some Lerp tutorials but I didn't understand how to use Lerp in this case."

    Vector3 has Vector3.Lerp. With that you can change your object transform position to make your object move from point A to point B:

    Code (CSharp):
    1. IEnumerator MoveFromTo(Vector3 from, Vector3 to, float speed, Transform tra)
    2. {
    3.     var t = 0f;
    4.  
    5.     while (t < 1f)
    6.     {
    7.         t += speed * Time.deltaTime;
    8.         tra.localPosition = Vector3.Lerp(from, to, t);
    9.         yield return null;
    10.     }
    11. }
    12.  
     
    kaiiboraka, Only4gamers and Boo-Let like this.
  6. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    You need a vector2 to declare the move to position. In your case x-1920 so

    Code (csharp):
    1.  
    2. Vector2 targetPos = new Vector2(1920, 0);
    3.  
    then in an enumerator you can use something like

    Code (csharp):
    1.  
    2. private IEnumerator Animate()
    3. {
    4.       var t = 0;
    5.       var target = transform.position - targetPos;
    6.  
    7.       while(t < 1f)
    8.       {
    9.             transform.localPosition = Vector3.Lerp(transform.localPosition, target, t);
    10.             t += time.deltaTime;
    11.             yield return null;
    12.       }
    13.  
    14.  
    something like that. Code may be off some.
     
    Last edited: Dec 14, 2020
    Only4gamers likes this.
  7. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    lol, I guess you updated while I was posting!! I like your method ;)
     
    Only4gamers likes this.
  8. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Thank you so much. I finally competed my code and it's now working exactly as I want. :)

    Code (CSharp):
    1. IEnumerator Next()
    2.     {
    3.         from = transform.localPosition;
    4.         to = new Vector3(transform.localPosition.x-1920, transform.localPosition.y, transform.localPosition.z);
    5.         tra = transform;
    6.         var t = 0f;
    7.  
    8.         previous.SetActive(false);
    9.         next.SetActive(false);
    10.    
    11.         while (t < 1f)
    12.         {
    13.             t += 1 * Time.deltaTime;
    14.             tra.localPosition = Vector3.Lerp(from, to, t);
    15.             yield return null;
    16.         }
    17.         tra.localPosition = to;
    18.  
    19.         previous.SetActive(true);
    20.         next.SetActive(true);
    21.  
    22.         if(transform.localPosition.x == -3840)
    23.         {
    24.             next.SetActive(false);
    25.         }
    26.         else
    27.         {
    28.             previous.SetActive(true);
    29.         }
    30.     }
     
  9. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Thank you so much for help :)
     
    Boo-Let likes this.
  10. Emreekurtt

    Emreekurtt

    Joined:
    Mar 21, 2022
    Posts:
    2
    I found something like this (for 2d)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MoveImage : MonoBehaviour
    4. {
    5.     public int stepSize;
    6.     Vector2 target;
    7.  
    8.     void Start()
    9.     {
    10.         target = new Vector2(gameObject.GetComponent<RectTransform>().anchoredPosition.x,
    11.             gameObject.GetComponent<RectTransform>().anchoredPosition.y);
    12.     }
    13.     public void Move()
    14.     {
    15.         target.x += stepSize;
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         gameObject.GetComponent<RectTransform>().anchoredPosition =
    21.             Vector2.Lerp(gameObject.GetComponent<RectTransform>().anchoredPosition
    22.             , target, Time.deltaTime * 2);
    23.     }
    24. }
    25.  
    its works with canvas elements.
     
    Only4gamers likes this.