Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 this right but basically I'm making a hunger system and I'm calling this in fixed update:

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

In this example, CurrentHunger starts as 100. Hunger rate is 15. My time.deltaTime is 0.02. With this 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 this?

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 which does exactly what you want.

Lerp is implemented like this:

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

while MoveTowards is implemented like this:

 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 $$anonymous$$axHunger 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 = $$anonymous$$athf.Lerp($$anonymous$$axHunger, 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.

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

Get positions at equal intervals between two Vector3 3 Answers

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

Linear optimization 0 Answers

How can I follow the terrain 1 Answer

How do i add "A" to "B" over a period of time? 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