• 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
3
Question by Griffo · Jan 19, 2014 at 11:21 AM · lerpmathf.lerp

Mathf.Lerp never gets there

Hi,

I want to lerp from 0 to 1 then back from 1 to 0, this is my code -

 // Only look at the player if within lookdistance
     if(distanceToPlayer < lookdistance){
         theLookAtPosition = player.position;
         ikLootAt.solver.IKPosition = theLookAtPosition;
         lerpWieghtUp();
         ikLootAt.solver.IKPositionWeight = lookWeight;
     }else if(distanceToPlayer > lookdistance){
         lerpWieghtdown();
         ikLootAt.solver.IKPositionWeight = lookWeight;
     }
 }
 
 function lerpWieghtUp(){
 
     while(lookWeight < 1.0f){
         lookWeight = Mathf.Lerp(lookWeight,1.0f,Time.deltaTime*lookSmoother);
         yield;
     }
 
 }
 
 function lerpWieghtdown(){
 
     while(lookWeight > 0.0f){
         lookWeight = Mathf.Lerp(lookWeight,0.0f,Time.deltaTime*lookSmoother);
         yield;
     }
 
 }

But lookWeight never reaches 0 or 1 so the while loop still keep going, how can I hit 0 and 1 with lerp?

Another way I can do this is -

 // Only look at the player if within lookdistance
     if(distanceToPlayer < lookdistance){
         theLookAtPosition = player.position;
         ikLootAt.solver.IKPosition = theLookAtPosition;
         lerpWieghtUp();
         ikLootAt.solver.IKPositionWeight = lookWeight;
     }else if(distanceToPlayer > lookdistance){
         lerpWieghtdown();
         ikLootAt.solver.IKPositionWeight = lookWeight;
     }
 }
 
 function lerpWieghtUp(){
 
     while( lookWeight < 1.0f ){
         lookWeight =+ 0.01f;
         yield;
     }
 }
 
 function lerpWieghtdown(){
 
     while( lookWeight > 0.0f){
         lookWeight =- 0.01f;
         yield;
     }
 }

But it looks false because of the sudden stoping at the ends, not gradually like lerp, any help would be appreciated, thanks.

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

2 Replies

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

Answer by instruct9r · Jan 19, 2014 at 11:52 AM

What i do, when using Lerp is add one IF statement which finish the Lerp when it gets realy close to it's target, cuz when finishing, the Lerp is slowing down and yes, sometimes it allmost never reach it's target value.

So what you can do is this:

 function lerpWieghtUp(){
  
     while(lookWeight < 1.0f){
        lookWeight = Mathf.Lerp(lookWeight,1.0f,Time.deltaTime*lookSmoother);
        if (lookWeight >= 0.95)
             lookWeight = 1;
        yield;
     }
  
 }
  
 function lerpWieghtdown(){
  
     while(lookWeight > 0.0f){
        lookWeight = Mathf.Lerp(lookWeight,0.0f,Time.deltaTime*lookSmoother);
        if (lookWeight <= 0.05)
            lookWeight = 0;
        yield;
     }


I am not testing the code in Unity, so i'm not sure if you have to put the If statement before or after the yield... Hope it helps :)

If 0.05 makes your object to snap too noticeable, you can decrease it to 0.025...

Comment
Add comment · Show 4 · 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 Griffo · Jan 19, 2014 at 12:05 PM 0
Share

instruct9r, thanks for the input, I'll accept your answer as correct because you pointed me in the right diction, thanks, this is what works for me -

 function lerpWieghtUp(){
 
     while(lookWeight < 0.99f){
         lookWeight = $$anonymous$$athf.Lerp(lookWeight,1.0f,Time.deltaTime*lookSmoother);
         yield;
     }
     lookWeight = 1.0f;
 }
 
 function lerpWieghtdown(){
 
     while(lookWeight > 0.02f){
         lookWeight = $$anonymous$$athf.Lerp(lookWeight,0.0f,Time.deltaTime*lookSmoother);
         yield;
     }
     lookWeight = 0.0f;
 }
avatar image kat0r · Jan 19, 2014 at 02:19 PM 1
Share

What both of you are doing wrong here, is that you do NOT lerp (linear interpolation), but use a bad quadratic interpolation. Look at your code, assu$$anonymous$$g you lerp from A to B, in .5 steps - if should be done in 2 steps, but your version NEVER reaches B. That is, because what you do is: lerp from A to (A + .5* AB) , then from THIS point again: (A + .5 (.5 AB)) and again (A + .5 (.5 (.5* AB))) etc.
If you want to do a real linear interpolation, you have to save your original position, and then do

 lookWeight = $$anonymous$$athf.Lerp(lookWeightOld,1.0f,Time.deltaTime*lookSmoother);


Simpler: you want to lerp from 0 to 1, in .5 steps.
You want: 0 .5 1
You actually do: 0 .5 .75 .875 ....

avatar image instruct9r · Jan 19, 2014 at 10:04 PM 1
Share

I don't get it. Where do you see (A + 0.5)? you Lerp from A (lookWeight) to 1.0. We never sum A (lookWeight) with 0.5..

Are you trying to say, that while Lerping, the lookWeight, as start point in the Lerp changes as well?

$$anonymous$$eaning when we start Lerping:

 lookWeight = $$anonymous$$athf.Lerp(lookWeight, 1.0, Time.deltaTime*lookSmoother);

which can be read in the begining as

 lookWeight = $$anonymous$$athf.Lerp(0.0, 1.0, Time.deltaTime*lookSmoother);

and somewhere at the middle of the Lerping the lookWeight in the brackets is also changing? like

 lookWeight = $$anonymous$$athf.Lerp(0.5, 1.0, Time.deltaTime*lookSmoother);


And even if this is true, then what does this have to do with the end value of the Lerp, which in this case is 1.0. It never changes...

avatar image kat0r · Jan 23, 2014 at 01:42 PM 1
Share

It is true, and your explanation is correct. But what you miss is, while the end of the lerp is 1.0, your lerp will NEVER reach 1.0 if the parameter is not 1, too. lerp(0.99, 1.0, 0.99) is still not 1.

avatar image
5

Answer by sed · Nov 18, 2014 at 04:55 PM

Well, the problem here is that you shouldn't be using just the Time.deltaTime in the lerp.

http://docs.unity3d.com/ScriptReference/Mathf.Lerp.html

Because here is how Lerp works: "Interpolates between a and b by t. t is clamped between 0 and 1."

So for the lerp to actually "get there" you need to maintain a counter

 function lerpWieghtUp(duration : float){
      var counter : float = 0.0f;
      while(counter < 1.0f){
          lookWeight = Mathf.Lerp(lookWeight,1.0f, counter);
 
          counter += Time.deltaTime / duration;
          yield;
      }
      lookWeight = 1.0f;
  }
 

and if you want it to look smoother you can use Mathf.SmoothStep

 function lerpWieghtUp(duration : float){
     var counter : float = 0.0f;
     while(counter < 1.0f){
         lookWeight = Mathf.Lerp(lookWeight, 1.0f, 
             Mathf.SmoothStep(0.0f, 1.0f, counter));
         
         counter += Time.deltaTime / duration;
         yield;
     }
     lookWeight = 1.0f;
 }


Duration here let's you specify how long should the animation take.

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 Serinx · Jul 14, 2017 at 01:41 AM 3
Share

Omg thank you. I know this was 3 years ago but I never knew how to lerp for a specified duration.

avatar image sed Serinx · Jul 14, 2017 at 08:17 AM 0
Share

Glad it helped. Too bad the accepted answer to this question is actually misleading.

And by the way, you could hit the vote up button next to the answer so I get internet points and eternal glory ;)

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

21 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

Related Questions

Making an object bounce, using Mathf.Lerp 1 Answer

Trying to lerp in different states in a switch statement 0 Answers

Is there a way to smoothly transition between two floats, with it slowing down towards the end 1 Answer

How the heck does Mathf.Lerp work? 2 Answers

Tracking a projectile with Mathf.Clamp 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