Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by jimjames · Dec 14, 2015 at 02:17 AM · lerp

Mathf.Lerp not working

With this script I am unable to get the "speed" var to pass 1.05. I am unsure why this is happening?

 public float speed;
 
 void Update ()
     {
         speed = Mathf.Lerp(1f, 8f, 0.5f * Time.deltaTime);
     {

Could someone clarify he reason? I have used Mathf.Lerp before and never got this effect before. I know the lerp slows down when it gets closer to the end var, but it s off by so much.

Comment
Add comment · Show 1
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 Positive7 · Dec 14, 2015 at 02:49 AM 0
Share
 speed = $$anonymous$$athf.Lerp(speed, 8.0f, Time.deltaTime * 0.5f);


3 Replies

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

Answer by Eric5h5 · Dec 14, 2015 at 02:57 AM

Lerp works fine (it's a very basic math function), but the way you're attempting to use it is wrong. Lerp stands for Linear Interpolation, where the function interpolates from one number to the next, where you normally have the third parameter advance from 0.0 to 1.0. If we assume Time.deltaTime is probably around 0.017 (60fps), then 0.5 * Time.deltaTime means 0.5 * 0.017 or 0.0085. So Lerp (1, 8, 0.0085) returns 1.0595 always, with some minor variation depending on the exact framerate.

The correct way to use Lerp is to not use Time.deltaTime in the third parameter; aside from the above, it makes your code framerate-dependent due to math which I won't get into here. The easiest way to use Lerp correctly is inside a coroutine (not Update):

 var t = 0.0f;
 while (t <= 1.0f) {
     t += 0.5 * Time.deltaTime;
     speed = Mathf.Lerp(1f, 8f, t);
     yield return null;
 }
Comment
Add comment · Show 8 · 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 Eric5h5 · Dec 14, 2015 at 03:02 AM 1
Share

BTW, Lerp is not supposed to "slow down at the end"; that's the opposite of Linear. It's the effect of using Lerp incorrectly. If you do want a slow-down effect, use the Sinerp function from here: http://wiki.unity3d.com/index.php?title=$$anonymous$$athfx

avatar image jimjames Eric5h5 · Dec 14, 2015 at 04:13 AM 0
Share

You misunderstood what I meant. I meant the rate the value is climbing to the end value, as closer the value gets the slower the rate of the value becomes. Also great answer. I thought couratines only worked with yieldforseconds and after reading about them, I am upset about not knowing how to work them before. They save a lot of unneeded variables Very helpfull answer.

avatar image Eric5h5 jimjames · Dec 14, 2015 at 04:31 AM 1
Share

No, I know exactly what you meant, but it's not the intended usage of Lerp.

Show more comments
avatar image IanGAV · Mar 12, 2017 at 07:26 PM 0
Share

O$$anonymous$$G, this is amazing. After looking for hundred of answers of how to use lerp, this is the only one that explains how lerp is really working. I thought that the point of using lerp is to get that slow down effect at the end. Now I realize that lerp doesn't work like that :). So if you want that effect at the end try this:

 while (elapsedTime <= time) {
 
             elapsedTime += 3*Time.deltaTime;
 
             gameObjectTodrag.transform.position = Vector3.Lerp (fromPos, toPos, $$anonymous$$athf.Sin(elapsedTime * $$anonymous$$athf.PI * 0.5f));
             yield return null;
         }


avatar image
3

Answer by TeohRIK · Dec 14, 2015 at 02:45 AM

Maybe you should try with this

  public float speed;
 
  void Update ()
      {
          speed = Mathf.Lerp(speed, 8f, 0.5f * Time.deltaTime);
      {
Comment
Add comment · Show 6 · 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 jimjames · Dec 14, 2015 at 02:49 AM 0
Share

Thanks. I knew it was something dumb. Btw, why wont it work the other way?

avatar image TeohRIK jimjames · Dec 14, 2015 at 02:57 AM 0
Share

you mean the one you posted?

Well, if base on my understanding. The starting value(the first parameter) didn't change every times it loop, so you keep getting the same starting value.

avatar image jimjames TeohRIK · Dec 14, 2015 at 03:52 AM 0
Share

Ahh... Thank you.

avatar image Eric5h5 · Dec 14, 2015 at 04:02 AM 0
Share

Note that this code will behave differently on machines with different framerates...not by a huge amount, but since it's easy enough to avoid it entirely, I'd strongly recommend not using it.

avatar image jimjames Eric5h5 · Dec 14, 2015 at 08:31 PM 0
Share

I thought that what "Time.deltaTime" was used for? Normalizing the differences between frame rate on different computers? or dose this not count for $$anonymous$$atf.Lerp?

avatar image Eric5h5 jimjames · Dec 14, 2015 at 09:40 PM 0
Share

If you're feeding Time.deltaTime back into itself, the framerate becomes part of the equation, which makes it dependent on framerate, not independent. With this code, if you have the framerate be 1fps, after 10 seconds, speed would equal somewhere around 6.7. If the framerate is 100fps, after 10 seconds, speed would be around 7.9.

--Eric

avatar image
-1

Answer by KaveeM2007 · Apr 25 at 05:08 AM

Guys, I just found a solution for this. when you assign the result of this (Mathf.Lerp(a,b,t)) you shouldn't do it to a new variable. for example If I want to set "k" to "M" by "t' in every frame using Mathf.Lerp. This is what I'd do - float K = 10; float M = 30;

 float t = 0.2f;
 
 Update()
 {
       Lerp();
 }
 
 void Lerp()
 {
       K = Mathf.Lerp(K ,M, t * Time.deltaTime)`
       float m = K;
       Debug.Log(m);
 }`

What happens is, I set the value returned by Mathf.Lerp to K which is the value I want to interpolate to ITSELF. By this way, in every frame K is updated. If you assign this returned value to a new variable, this K will stay same forever and will give the same value every time you run the code as it is not changing withing the Mathf.Lerp method. this method does not change it's arguments.. But, by assigning the returned value of the method to itself K is UPDATED EVERY FRAME. then you won't stuck in the same value every time you run your code. If you want this K value for another variable, assign it just after the method. (I've done it as a comment) Debug.Log is the way how I took output here.

Comment
Add comment · 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

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

34 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

Related Questions

Moving player in an arc, from startPoint to endPoint 2 Answers

Make Lerp or other more fluid or continuous 3 Answers

Collision.contacts? 1 Answer

Stop a Lerp from looping 3 Answers

custom follow script choppy movement 2 Answers


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