• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by punkrockchicken · Sep 06, 2016 at 07:24 PM · transform.positiontranslate

How to smoothly move object with RectTransform?

Hello! I am new to Unity and don't understand why I cannot find an easy way to smoothly move a gomeObject a little bit to the left.

I want to move a button which for some reason does not have Transform component, but it has Rect Transform component(attached)

All I need is to change Pos X value to '-42', but I don't know how. I am trying this:

 Vector3 newPos = new Vector3(-42, 0, 0);
 faceButton.transform.position = Vector3.Lerp(faceButton.transform.position, newPos, Time.deltaTime * 2.0f);

But it moves the button to position (-58.9, 24.48, 0) and it happens immediately So I am pretty confused. Could anyone help please?

Thank you.

снимок.png (12.7 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kaweri · Oct 07, 2020 at 06:36 AM 0
Share

thank you, its working

2 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by unityBerserker · Sep 06, 2016 at 08:37 PM

@punkrockchicken

More about Lerp you can read here:

https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/

When you using Lerp you must know that last parameter is normalized (Max value should be equal 1). We end moving when we reach 100% our way. Beginning position is equal to 0 and ending position is equal to 1. You can you set this for example like this:

      float timeOfTravel=5; //time after object reach a target place 
     float currentTime=0; // actual floting time 
     float normalizedValue 
     RectTranform rectTransform = GetComponent<RectTransform>(); //getting reference to this component 
     
     IEnumerator LerpObject(){ 
     
     while (currentTime <= timeOfTravel) { 
     currentTime += Time.deltaTime; 
     normalizedValue=currentTime/timeOfTravel; // we normalize our time 
 
 rectTransform.anchoredPosition=Vector3.Lerp(startPosition,endPosition, normalizedValue); 
     yield return null; }}

What happen if we have normalizedValue greater then 1? We go further and miss endPosition.

How to run Coroutine? First cache it with IEnumerator cachedCoroutine =LerpObject(); Next use StartCoroutine(cachedCoroutine); With coroutine this will move smoother.

Why I used rectTransform.anchoredPosition?

If you use transform at different resolution you will have different position in UI. AnchoredPosition is always in the same position even if we change resolution. In UI element we using ratio not actual position of element(that why we have hooks).

(If you use Canvas (Component) - Render mode - World Space - you should use transform.position like normal. In other Canvas modes use this.)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image punkrockchicken · Sep 06, 2016 at 10:11 PM 0
Share

Thanks for such a great explanation!

avatar image roos1600 · May 19, 2019 at 07:00 AM 0
Share

Can you explain me how you defined "startPosition' and 'endPosition'?

avatar image
4

Answer by metalted · Sep 06, 2016 at 08:37 PM

I just tested a piece of code for this. Hope this will help you.

     //Declare RectTransform in script
     RectTransform faceButton;
     //The new position of your button
     Vector3 newPos = new Vector3(-42, 0, 0);
     //Reference value used for the Smoothdamp method
     private Vector3 buttonVelocity = Vector3.zero;
     //Smooth time
     private float smoothTime = 0.5f;
 
     void Start()
     {
         //Get the RectTransform component
         faceButton = GetComponent<RectTransform>();
     }
    
     void Update()
     {
         //Update the localPosition towards the newPos
         faceButton.localPosition = Vector3.SmoothDamp(button.localPosition, newPos, ref buttonVelocity, smoothTime);
     }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to teleport gameobject (instantly change transform.position) 3 Answers

Moving a GameObject to a certain point in world space via script 1 Answer

Using transform.position and Mathf.PingPong on an object how have a random x position 1 Answer

Getting Vector3 in between two given Vector3 after given Distance. 2 Answers

translate a gameobjects position.y based on another gameobjects position .z 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges