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 playajin · Mar 28, 2015 at 04:54 PM · mathf.lerp

C# Adding and counting up scores with Mathf.Lerp

Hello. I'm seriouisly spending too much time solving this and still stuck in here...

my project is about obtaining land as much as you can and the player has only 3 chances.

everytime the player gain land I want to count up from the last score to current score(last score + land I just obtained)

I'm using Mathf.Lerp and here it is.

 private float land; //the score from each tries
 private float totalScore; //the score combined
 
 totalScore += Mathf.Lerp (0, land, Time.deltaTime * 0.5f);
 slider.value = _totalScore; //a slider that shows current score.
 

Q01:

I put the Lerp method in Update() Function and it didn't work.

(totalScore kept going up and didn't stop)

if I put it in separated function, Lerp works only once showing very small value.

Q02:

after 1st try, float "land(ths land I just obtain)" is not the value that this Lerp should end with.

this Lerp shoud start from the last value to current value.

how should I set from & to values in Lerp insted of 0 & land?

someone please help me out of this :) thank you for reading anyway!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by NoseKills · Mar 28, 2015 at 08:14 PM

Did you read how Matf.Lerp works and what the parameters mean? It simply returns a value between the first two parameters based on the third parametes. If the third parameter is 0, it returns the first parameter. If third parameter is 0.5f, it returns the value that sits at the halfway point between the first two parameters.

You are feeding in Time.deltaTime *0.5f which means that in every update you increase totalScore by some value that's (depending on framerate) a few percent from 0 towards the value of land. Actually the value you add is Time.deltaTime *0.5f * land since your first parameter is 0.

What you probably want to do is store the currently shown score and the new score whenever you get more points and set a timer float to 0. Then in update Lerp it like

 float pointAnimDurationSec = 2f;
 float pointAnimTimer = 0f;

  // var for storing the actual current score.
 // end point of the Lerp
 float currentScore = 0;

 // var for storing the score just before points were last added
 // start point of the Lerp
 float savedDisplayedScore = 0;

 // a variable for the "animated" score you should show in the UI.
 // We can't put the result of Lerp into
 // the vars above because that would mess up the result of the next Lerp
 float displayedScore = 0;

 // ^^ Added the word "displayed" because neither 
     // of these mark how many points th player really has.
 // Afterall you animate the score just to make it pretty

 void AddPoints(float points)
 {
     // A
     // what if you get more points before last points finished animating ?
     // start the animation again but from the score that was already being shown
     // --> no sudden jump in score animation
     savedDisplayedScore = displayedScore;

     // B
     // the player instantly has these points so nothng gets 
     // messed up if e.g. level ends before score animation finishes
     currentScore += points;
     // Lerp gets a new end point

     pointAnimTimer = 0f;
 }
 
 void Update ()
 {
     pointAnimTimer += Time.deltaTime;

     float prcComplete = pointAnimTimer / pointAnimDurationSec;

     // don't modify the start and end values here 
     // prcComplete will grow linearly but if you change the start/end points
     // it will add a cumulating error
     displayedScore = Mathf.Lerp(savedDisplayedScore, currentScore, prcComplete);
 }

With this code animating the new points always takes 2 seconds. Add a miltiplier to Time.deltaTime to get a slower or faster animation

Comment
Add comment · Show 7 · 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 playajin · Mar 29, 2015 at 09:29 PM 0
Share

hey thank you I understand how $$anonymous$$athf.Lerp works! :)

thanks to your "+= Time.deltaTime" solution, now the score counts up fine but it starts from the value I just make.

(because I don't understand your SavedDisplayedScore part)

I' been studying C# for 5 month and digging a book but it's not easy :(

I set total score with += and used in Lerp like below.

 float score; //score I get on each tries.
 float totalScore += score; //to pile up scores 
 float timerFloat += Time.deltaTime;
 _totalScore = $$anonymous$$athf.Lerp (score, totalScore, timerFloat);



Like you said, I should store totalScore(before I get new score and add it) and use it as the first parameter in $$anonymous$$athf.Lerp.

but I can't think of right way to do so.. more explanation would be truly appreciated :)

avatar image NoseKills · Mar 29, 2015 at 10:34 PM 0
Share

$$anonymous$$odified my answer and added comments. I named the variables like that to signify that those variables are for animation purposes only. So you should never save those anywhere.. otherwise you might for example save a smaller score just because the animation hadn't finished when the level ended.

It's always a good thing to keep UI variables and logic separate from your game's core logic. From the game logic's perspective currentScore is the only relevant variable. Don't use it for anything else.

Also, there was for example the issue of what happens when you get more points when the score is still animating the previous addition. Do you instantly jump to the new lerp start score and animate from there to actual score or do you just increase the target score so there's no jump in the score... I did the latter.

$$anonymous$$ostly a few issues that you possibly didn't think about yet because you didn't get that far

avatar image playajin · Mar 29, 2015 at 11:47 PM 0
Share

wow I just can't thank you enough for this. I'll try it and let you know how it worked!

avatar image playajin · Mar 29, 2015 at 11:51 PM 0
Share

and I wasn't even aware of the issue you mentioned and I think second one(no jumping) is much better too.

avatar image playajin · Mar 30, 2015 at 11:53 AM 1
Share

now it's working :) thank you so much!

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to Lerp for a number to get BIGGER instead of smaller? 1 Answer

how to use time on lerp 2 Answers

Continuing movement after animaton on GetButtonUp 0 Answers

Mathf.Lerp happens instantly 2 Answers

Mathf.lerp not finishing the lerp(age) 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