0

I just wanted to know how do you transition a UnityEngine.UI from one position to another smoothly?

My current code is this:

GameObject rank1;
GameObject rankSlot1;

rank1 = GameObject.Find("Rank1");
rankSlot1 = GameObject.Find("RankSlot1");

rank1.transform.position = new Vector3(Mathf.Lerp (rank1.transform.position.x, rankSlot1.transform.position.x, 0.1f), rankSlot1.transform.position.y, 0);

But it seems like the Mathf.Lerp doesn't work :/

Thanks!

EDIT: All these gameobjects do have Rect Transform as they are a children of Canvas

2 Answers 2

1

It depends where do you use Lerp ? Are you calling it once ? or in Update(). Calling in update with correct parameters should work.

Beside Lerp, you can use Animator component with position curves to move from one point to another.

There is another option to use LeanTween plugin, it is a free plugin on asset store.

LeanTween.move(gameObject, yourFinalPosition, duration);
3
  • Hi there, I'm calling it at Start method. Do I need to put it at Update? I can't use Animator because my object needs to animate itself to various places. Lastly, I don't want to use a plugin. Thanks :)
    – Jim
    Commented Aug 13, 2015 at 5:27
  • Then try this in Update. Start calls only once. Update calls on each frame so change in parameter will done on frame basis. Commented Aug 13, 2015 at 6:16
  • It works on inside the Update! I'm curious how Math.Lerp does that? Like it knows where it left from the previous frame. I'm just a newb. Anyway, thanks bro! EDIT: IDK how to mark the comment as the correct answer so I just instead marked the whole answer
    – Jim
    Commented Aug 13, 2015 at 6:54
0

If using an animator you get access to the animation curves so you can get really nice controlled animations Tricky part is if you are wanting to use 'apply root motion' (have the animations start where they are instead of fixed positions) you need to apply it to something that is considered ROOT: - create an empty gameobject - with a normal transform - parent the canvas with text to this new gameobject - apply the animations to the gameobject transform ( not the UI rectTransform) - on the animation in the inspector you should now be able to click on the 'generate root motion curves' and also select the 'apply root motion' check box for the Animator

hope this helps,, took a few hours to get working :) I used this to float up enemy damage text displays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.