Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
17
Question by Posly · Apr 09, 2012 at 02:31 AM · vector3lerpmathfvector3.lerpmathf.lerp

How the heck does Mathf.Lerp work?

What does it exactly do, the scripting reference doesn't quite give me the best explanation. I'm trying to get it to move a character from "point a" to "point b" over a set amount of time. However it doesn't seem to work like that... Anyways can someone sum up what exactly lerp is and how it works? Thank!

Comment
Add comment · Show 2
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 · Apr 09, 2012 at 02:47 AM 0
Share

http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html

avatar image marklin · Nov 26, 2014 at 01:38 AM 2
Share

I like the title you gave to your question. The docs fail epically and it's frustrating.

2 Replies

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

Answer by Kleptomaniac · Apr 09, 2012 at 02:45 AM

Yes, you're right. The docs on Mathf.Lerp are horrible. I'm guessing you're using Time.time as your t parameter like in the docs? Bad idea.

Basically, lerp stands for linear interpolation. It means that the value will change from your from value to your to value over t. The t parameter is set within a range of 0 to 1, 0 being your from value, 1 being your to value. Therefore, 0.5 will be halfway between.

In this way, if you were to set it up like this:

 var tParam : float = 0.5;
 var valToBeLerped : float = 0;
 valToBeLerped = Mathf.Lerp(0, 3, tParam);

valToBeLerped would equal 1.5. This means that you can have a changing t parameter to have a value which linearly interpolates between two values. So like this:

 var tParam : float = 0;
 var valToBeLerped : float = 0;
 var speed : float = 0.3;
 if (tParam < 1) {
     tParam += Time.deltaTime * speed; //This will increment tParam based on Time.deltaTime multiplied by a speed multiplier
     valToBeLerped = Mathf.Lerp(0, 3, tParam);
 }

This will gradually fade the valToBeLerped variable between 0 and 3 proportional to t.

The problem with using Time.time, however (as it is used in the docs) is that, since the t parameter is based on a range between 0 and 1, and Time.time is the amount of seconds since the startof runtime, your Lerp will only ever work once in the very first second of the game. Therefore, it is best to stay away from Time.time in Lerp functions, unless of course you only want your Lerp to work in the first second of the game for some reason.

This can be helpful ... although it's not directly compatible with Unity's Mathf class, it will still maybe help you to understand how Lerp actually works better than the Script Ref can. Also have a look at the Wikipedia definition of linear interpolation to gain a better understanding of the graphical meaning of lerping.

I hope that helps, Klep

Comment
Add comment · Show 5 · 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 Posly · Apr 09, 2012 at 03:04 AM 0
Share

I cannot thank you enough! Again thanks!

avatar image Kleptomaniac · Apr 09, 2012 at 03:14 AM 0
Share

No worries! I had trouble understanding Lerp at first ... so I'm glad I could help someone out. :)

avatar image DNP · Sep 01, 2012 at 02:17 AM 0
Share

Amazing tutorial. Great job!

avatar image MrSteve1 · Oct 15, 2013 at 07:34 PM 0
Share

such a great explanation. Unity Docs makes it real difficult to understand. This has been bugging for me years. Thank you soooooooo much. Look at how many "O"'s I used. You know I mean it,

avatar image wasicool7 · Aug 21, 2016 at 09:09 PM 0
Share

Hi, would it be possible to convert this into c# please. Thank you in advance :)

avatar image
0

Answer by kolban · Apr 09, 2012 at 02:39 AM

Lerp interpolates from one value to another over a given range. A pretty good tutorial video on this topic can be found here:

https://vimeo.com/channels/151501/17070181

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 jc_lvngstn · Sep 04, 2012 at 03:03 PM 0
Share

I wish UT had time to add such awesome examples and explanations to their documentation :)

avatar image RealSoftGames · Jul 07, 2015 at 01:41 PM 0
Share

although i am running into an issue with this lerp works fine in Update, but not in a function call other than update as the function only gets called in 1 frame. lerp happens over many. if my understanding is incorrect, could someone please correct me, i find Lerp Doc's to be very messy although other documentation is rather very nice.

avatar image Eric5h5 · Jul 07, 2015 at 02:25 PM 0
Share

Lerp does not happen over many frames. It's a very basic math function, that returns a value immediately. If you want progression, you need to call it repeatedly while increasing (or decreasing) the value of t, the third parameter.

avatar image TooManySugar Eric5h5 · Jul 09, 2018 at 12:38 PM 0
Share

Hey, I read a similar answers from you back in teh days. I know this post is old but is still on the very top of google searches for Lerp. While the objective of lerp is to linearly interpolate between two known values using an increasing variable "t". There are other situations where t may have a given value and one of the values is the one that varies. I can´t find your answer now but you whent against a time.deltatime like value in t somewhere in Unity Answers. Its true that it defeats a bit the purpose of the fuction but there are situations where you whant to smoothen the movement (interpolate) of a thing where the positions you whant to interpolate are not known and t is used just as time.deltatime*speed. And to me that is perfectly ok. (this all running in Update). Funny enough the examples in unity video tutorials use the time.deltatime for t when interpolating the ball. It is pretty common to see the deltatime approach in camera smoothing scripts etc.

avatar image Eric5h5 TooManySugar · Jul 09, 2018 at 04:28 PM 1
Share

It's not O$$anonymous$$, because it makes the movement framerate-dependent. Unity tutorials aren't known for always being correct when it comes to coding.

avatar image _Gkxd · Jul 07, 2015 at 02:32 PM 1
Share

Lerp(a, b, t) is essentially shorthand for a*(1-t) + b*t where t is clamped between 0 and 1. It does only that, nothing else.

 float c = Lerp(a, b, t);
 float d = a*(1-t) + b*t;
 // c == d, assu$$anonymous$$g t is between 0 and 1

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

19 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

Related Questions

how to use time on lerp 2 Answers

Vector3.Lerp completing before t = 1? 2 Answers

Vector3.Lerp moves in the wrong direction 1 Answer

Vector3.Lerp isn't working 3 Answers

Making an object bounce, using Mathf.Lerp 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