Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by yapaysinek · Aug 06, 2016 at 12:19 PM · coroutinelerpsmoothintmathf

Lerp between two int vaules smoothly

Hey Unity Pros,

so ive been trying to get this work since last night and it has given me hours of headache since because i dont know what is the problem or the bug in my code.

Basically I want to lerp from one int (eg.:1000points) to another (eg.:800points) using lerp to give that smoothing effect that it makes because its displayed to the player using UI Text.

The problem is that it doesnt work correctly or the way i want it to. It does weird things when I run the function such as showing me random numbers or just staying at 999points the whole time.

Here is an example of my code:

 public void startSubtracting () {              //also.. this is running in Update
 
     score = 1000;
     score = (int)Mathf.Lerp (score, 800, subtractSpeed * Time.deltaTime);
     scoreText.text = score.ToString ();
 }

I'd highly appreciate it if you guys could help me and thanks in advance!

Comment
Add comment
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

4 Replies

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

Answer by yapaysinek · Aug 06, 2016 at 04:22 PM

So after struggling for a few more hours I managed to get the results I was looking for! (yaay:D) So I decided to post the little code that worked for me if someone else will need it in the future:) Thanks for the answeres tho!:)

 float lerp = 0f, duration = 2f;
 
     public void startSubtracting () {
 
         score = 1000;
         scoreTo = 800;
         lerp += Time.deltaTime / duration;
         score = (int)Mathf.Lerp (score, scoreTo, lerp);
         scoreText.text = score.ToString ();
     }

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 GunLengend · Sep 13, 2017 at 05:32 AM 0
Share

Thank for ur sharing.

avatar image Skaiji-dev · Nov 26, 2021 at 07:53 AM 0
Share

Thanks! :P

avatar image
2

Answer by DavidSof · Dec 24, 2017 at 10:01 PM

In my case the best solution for this is using vectors

 using System.Collections; //for Vector3.Lerp
 using UnityEngine.UI;
 
 Vector3  score;
 Vector3  target_score;
 public Text score_text;
 
 void Start () {
 score= new Vector3 (0, 0, 0);
 target_score= new Vector3 (100, 0, 0); // we run score to 100 points
 }
 void Update () {
 score=Vector3.Lerp(score,target_score,Time.deltaTime);
 score_text.text=(Mathf.RoundToInt(score.x)).ToString();
 }

it's simple and nicely and smooth

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
avatar image
0

Answer by fantom642 · Aug 06, 2016 at 12:56 PM

The third parameter in the Lerp method needs to be a float between 0 and 1.

 int t=0; 
 public void startSubtracting () {              //also.. this is running in Update
      score = 1000;
      score = (int)Mathf.Lerp (score, 800, t);
      t += subtractSpeed * Time.deltaTime;
      if(t>1)
           score=800;
      scoreText.text = score.ToString ();
  }

For more details see example here: https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html

Comment
Add comment · Show 1 · 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 yapaysinek · Aug 06, 2016 at 02:29 PM 0
Share

Hey fantom642,

thanks for the answer! I have tried your solution.. so if i set the subtractSpeed to 2f than the the score will "jiggle" between the 999 and 997.. if i remove time.deltatime it goes to 800 instantly.. no smoothing:( if i set the subtractSpeed to 0.1f for example i get 980(which is the 10% of the difference between the two ints.. oh and still no smoothing):(((

avatar image
0

Answer by JasonSpine · Aug 05, 2019 at 09:31 AM

It's a bit old thread, but it may help others looking for a solution. I came up with the following formula:

         int IntLerp(int a, int b, float t)
         {
             if (t > 0.9999f)
             {
                 return b;
             }
 
             return a + (int)(((float)b - (float)a) * t);
         }
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.

Follow this Question

Answers Answers and Comments

73 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 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 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 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 avatar image

Related Questions

How can you increase a float to certain number and stop, while it is being used. 2 Answers

Slowly increase or decrease between two int values? 0 Answers

Final position of the camera movement 0 Answers

How to smoothly rotate gameobject 20 degrees on key press? 0 Answers

Why my angle lerp code take only 30% time to finish? 0 Answers


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