Can't tween float
« on: September 20, 2015, 12:23:03 AM »
I have a float variable called balance and I show it in a 2d Toolkit TextMesh. Right now, Im doing something like this to change it:
Code: [Select]
balance += totalPayment;
UpdateUI();

But I don't want to change the value from A to B. Let's say balance = 50 and totalPayment = 150, I want to increase it 50,51,52,53 (or something like that) until 200 (balance + totalPayment). So the closest I got is this:

Code: [Select]
float to = balance + totalPayment;
DOTween.To(() => balance, x => balance = x, to, 2).OnComplete(UpdateUI);

Nevertheless, this doesn't work as expected. It doesn't do what I need, it just wait 2 seconds and then change the value of balance to balance+totalPayment. What I'm doing wrong?

EDIT: UpdateUI just take does this: txtBalance.text = balance.ToString("F2"); 
« Last Edit: September 20, 2015, 12:31:34 AM by DarkSlash »

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: Can't tween float
« Reply #1 on: September 20, 2015, 01:27:09 AM »
Hi,

You code is absolutely correct and should do the animation you intend. I'm sure there's no bugs in float tweens, so maybe there's something else blocking your tween from happening? Like a method somewhere setting "balance" continuously, or Time.timeScale being set to 0 (in which case you will have to chain a SetUpdate(true) to make the tween independent).

Let me know.

Cheers,
Daniele

Re: Can't tween float
« Reply #2 on: September 20, 2015, 01:55:28 AM »
Im not setting balance from anywhere else and my timeScale is the default. I have this on Start():  DOTween.Init(); do I have to do something else? Attach some script to the gameobject? Or initialice DOTween in another way? I'm calling DOTween.To(...) from a function, do I have to do it from Update() or somewhere special?

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: Can't tween float
« Reply #3 on: September 20, 2015, 03:05:01 AM »
No you can call it wherever you want. Are you sure that "balance" is not animated? Are you calling something like UpdateUI every frame, to update the UI with the value of "balance"?
Code: [Select]
DOTween.To(() => balance, x => balance = x, to, 2).OnUpdate(UpdateUI).OnComplete(UpdateUI);

Re: Can't tween float
« Reply #4 on: September 20, 2015, 03:06:15 PM »
Oh, I though that OnComplete was called every time it completes doing every step in the tween but its called when the whole tween is complete. My mistake ;)

Also, there's a way to limit the decimals to just 2 in the floats? Or, if the number has not decimal part (1080), tween without decimals, if the number has decimals (1080.75) tween with decimals. Can this be done?
« Last Edit: September 20, 2015, 03:14:15 PM by DarkSlash »

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: Can't tween float
« Reply #5 on: September 20, 2015, 03:13:10 PM »
Ah, glad we solved the mystery.

About rounding floats, you could convert them to an int inside UpdateUI, before setting the string. Decimals can't be limited on a float otherwise. That said, when the tween stops the value should be exactly 1180 :O I checked and that works on my side.

Re: Can't tween float
« Reply #6 on: September 20, 2015, 03:15:39 PM »
Sorry, I edited my post while you were replying, I missed the last txtBalance = balance.ToString(); in the OnComplete function ;)
Ok, I can convert to int, tween them, and then add the decimal part. That's the only way?

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: Can't tween float
« Reply #7 on: September 20, 2015, 03:31:45 PM »
Ah no then. If you want to keep "up to" two decimals, then leave the float as is, and just convert it when you cast it to string:
Code: [Select]
txtBalance = String.Format("{0:0.##}", balance);

Re: Can't tween float
« Reply #8 on: September 20, 2015, 03:36:55 PM »
Yes I think is the best solution. I got decimals when I tween a round value, but if only two decimals it doesn't look bad. I do ToString("F2") insted of Strinfromat

Thank you for your help!!

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: Can't tween float
« Reply #9 on: September 20, 2015, 03:47:00 PM »
Great :) Though, if I'm not confused, with that String.Format you shouldn't get any decimal at all if the value is round, and up to two if it's not

Re: Can't tween float
« Reply #10 on: September 20, 2015, 03:48:44 PM »
Oh! Thanks!