• 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 nerdares · Jul 21, 2016 at 11:37 PM · lerpmathdecreaselinearinterpolate

How to lerp with a changing "from" value?

It's possible I'm not even using t$$anonymous$$s right but basically I'm making a hunger system and I'm calling t$$anonymous$$s in fixed update:

    public void Starve()
    {
         if(CurrentHunger > 0)
         {
           CurrentHunger = Mathf.Lerp(CurrentHunger, 0, HungerRate * Time.deltaTime);
         }
 
     }

In t$$anonymous$$s example, CurrentHunger starts as 100. Hunger rate is 15. My time.deltaTime is 0.02. With t$$anonymous$$s use of lerp, basically, my from value is always changing.

So:

  • 1 second it is 70

  • 2 seconds it is 49

  • 3 seconds it is 34.3

  • 4 seconds its is 24.01

See? It's not decrementing linearly. Am I even using lerp right? And what do I need to do to fix t$$anonymous$$s?

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
0
Best Answer

Answer by Bunny83 · Jul 21, 2016 at 11:52 PM

Lerp won't work in that case. Both values need to be constant.

Instead of Lerp you might want to use Mathf.MoveTowards w$$anonymous$$ch does exactly what you want.

Lerp is implemented like t$$anonymous$$s:

 public static float Lerp(float a, float b, float t)
 {
     return a + (b - a) * Mathf.Clamp01(t);
 }

w$$anonymous$$le MoveTowards is implemented like t$$anonymous$$s:

 public static float MoveTowards(float current, float target, float maxDelta)
 {
     if (Mathf.Abs(target - current) <= maxDelta)
     {
         return target;
     }
     return current + Mathf.Sign(target - current) * maxDelta;
 }


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 nerdares · Jul 22, 2016 at 12:22 AM 0
Share

I also think I have found a solution:

I already had a value called MaxHunger which is 100;

I made a new value called currentHungerDelay. This will help update the third parameter in the lerp section as I need to keep the values of the first two parameters constant, like you said.

Therefore, I set currenthunger equal to the new lerp value and I made sure current hunger was not part of the parameters in there. Here's the code

 public void Starve()
     {
         currentHungerDelay += Time.deltaTime;
         if (currentHungerDelay >= HungerRate)
             currentHungerDelay = HungerRate; 
 
 
         float t = currentHungerDelay / HungerRate;
 
         CurrentHunger = Mathf.Lerp(MaxHunger, 0, t);
         
     }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

54 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

Related Questions

Something like Color.MoveTowards ? 2 Answers

How to change a value over specified time? 2 Answers

Are there any easing functions built into Unity 5? 1 Answer

Linear optimization 0 Answers

How can I follow the terrain 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