Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by MDarkwing · Sep 09, 2013 at 02:04 PM · lerphowworkexplanation

Explanation of Lerp()?

Ok if someone can please explain it, because I'm having trouble understanding it. I am not sure how my calculations aren't similar to the effect in Unity when i run the project. As i read the linear interpolation takes is based on formula (y-y0)/(x-x0) = (y1-y0)/(x1-x0) given we have two point A(x0,y0) and B(x1,y1). Also I have found that for singular values formula for Lerp is x0 = from, x1 = to, t = fraction so the formula is **x0 + (x1-x0)t*. So basically I want to use Lerp between values 0-90. Based on Unity documentation on Lerp and also the tutorial example I've seen they use the Time.deltaTime as t usualy multiplied with some smooth factor. My question is next:

 float startValue;
 float resultValue;
 float smooth = 4f;
 
 void Update()
 {
    if(Input.GetKeyDown(KeyCode.A)){
       resultValue = 0f;
       toValue = 90f;
    }
    resultValue = Mathf.Lerp(resultValue, toValue, smooth*Time.deltaTime);
 }


The Time.deltaTime is as I get it now around ~0.02 sec multiplied with 4 that's 0.08~0.09. Before trying to apply the formula, based on documentation and some posts I read the value of t is between 0-1. If it's 0.5 it returns half way of between values, so between 0 and 90 it should return 45. If t=0.08 is that 8% of the 90? If that so it should be 7.2 for first iteration of Update(). Instead I get value of 5.96?!? Next I tried to use formula it is the same as my fist thought. So how does it work? Also I've noticed there is a few scenarios of using the Lerp(), one like this that I'm using, the one where you have start time then calculate t based on maxValue/currentValue, and I think there was another one but i cant remember now.

Thank you in advance, cheers!!! :)

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

3 Replies

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

Answer by Peter G · Sep 09, 2013 at 03:18 PM

The general equation for linear interpolation is your second one.

  Output = (1 - t) * Input1 + t * Input2

It can nicely be expanded to higher degrees (see Bezier curves) and vectorized. Just treat the vector as a collection of parallel interpolations.

As to why your value is slightly off, your Time.deltaTime for the first frame was probably just a little faster than 20ms. Really I wound't focus that much on it unless you need exactly 8%.

If you need to calculate t, then Unity has a built-in fuction for that:

 t = Mathf.InverseLerp ( Input1 , Input2 , Output);

and the actual calculation for that looks something like:

 t = (Output - Input1) / (Input2 - Input1)
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
avatar image
0

Answer by Kavorka · Sep 09, 2013 at 03:43 PM

Time.deltaTime is not fixed to 0.02 seconds. It depends on the frame rate. Your Time.deltaTime was about 0.015 seconds and your frame rate was about 68 frames per second. Lerp works as you explain but not Time.deltaTime.

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
avatar image
0

Answer by MDarkwing · Sep 09, 2013 at 04:37 PM

Thanks guys, @Kavorka yeah i got that, that was indeed my miscalculation after i checked it. :D Either way I read some more posts and explanations and now I totaly get it what happens in Lerp based methods.

@Peter G, thank you man for that additional info it's so damn useful, and for real formula also :)

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

17 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

Related Questions

How does the Unity Karma and Rank work? 1 Answer

how "Animation" work? 1 Answer

How to work out system requirements 1 Answer

How To Use Slerp 1 Answer

"Unknown resolve error" 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