Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by pcjacob · Nov 22, 2013 at 09:11 PM · animationtextlerplabeldivision

Best way to animate incrementing/decrementing values ?

I am trying to animate text where numerical values don't simply just go from value A to value B but increment/decrement quickly over a set amount of time (meaning a set amount of frames). For example, if the game is at 30 frames per seconds and I go from 10 hit points to 40 points, I would like the hit point counter to show each increment of the hit point value (31, 32, 33 ... 38, 39, 40) over a 1 second period.

I created a simple script that can be attached to any gameObject that wants animated text:

using UnityEngine; using System.Collections;

 public class TextUpdater : MonoBehaviour {
 
     private UILabel _label;
     private int _initialValue = 0;
     private int _finalValue = 0;
     private string _format = null;
     private int _duration = 0;
 
     private float timeElapsed = 0.0f;
 
     public void Set(UILabel label, int initialValue, int finalValue, int duration, string format = null)
     {
         _label = label;
         _initialValue = initialValue;
         _finalValue = finalValue;
         _duration = duration;
         _format = format;
 
         _label.text = _initialValue.ToString(_format);
     }
 
     void Update()
     {
         timeElapsed += Time.deltaTime;
 
         if(timeElapsed > _duration)
         {
             _label.text = _finalValue.ToString(_format);
             Destroy(this);
         }
         else
         {
             float value  = Mathf.Lerp(_initialValue,_finalValue, timeElapsed/_duration);
             _label.text = value.ToString(_format);
         }
         
     }
 }

And here is a simple use of the script :

 using UnityEngine;
 using System.Collections;
 
     public class hitPointHuD : MonoBehaviour
     {
         private UILabel __hitpointsLabel;
         private int _hitpoints = -1;
         
         // ------------------------------------------------------------
         // Get reference to instances
         // ------------------------------------------------------------
         void Start ()
         {
             __hitpointsLabel = GetComponent<UILabel>();
         }
         
         // ------------------------------------------------------------
         // Update label if hitpoint changes
         // ------------------------------------------------------------
         void Update ()
         {
             if (_hitpoints != Level.Instance.hitpoints)
             {
                 int previousHP = _hitpoints;
                 _hitpoints = Level.Instance.hitpoints;
     
                 gameObject.AddComponent<sbLabelUpdater>().Set(__hitpointsLabel, previousHP, _hitpoints, 2, "N0");
             }
         }
     }

Now this code works fine and it gives me the exact effect I want. What I do not like is the division ("timeElapsed/_duration") in the update call. Being where it is, this division will occur every frame for every UILabel that is being updated. I don't see how I could use Lerp without the ratio I get from the division. Do note that I do not absolutely want to use Lerp at all costs, it was simply the 1st solution that came to my mind.

I know I am not the first to want to create this effect... yet since it does not have an official name it has been quite hard to find examples. Does anyone know a better way to create this effect ?

Thanks !

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 Eric5h5 · Nov 22, 2013 at 09:21 PM 0
Share

A division per Update is utterly trivial and not worth even thinking about. Especially on modern CPUs; on $$anonymous$$e + - * / all benchmark as the same speed, and even a square root is only a few times slower (allowing for function call overhead).

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

18 People are following this question.

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

Related Questions

Lerping Field Of View is buggy 1 Answer

UI Text Alpha not fading out in animation 0 Answers

Can You Animate Main Menu Title? 2 Answers

Text is too small while displaying in device 1 Answer

Unity 5 - UI Text 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