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!
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 ();
     }
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
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
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):(((
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);
         }
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                