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 /
  • Help Room /
avatar image
0
Question by GangBlader · Mar 19, 2018 at 12:57 AM · lerptutorial

Linear Interpolation that is independent of frame rate

In this Tutorial (https://unity3d.com/learn/tutorials/topics/scripting/linear-interpolation?playlist=17117) , the last example is this:

 > void Update () {
 >     light.intensity = Mathf.Lerp(light.intensity, 8f, 0.5f *
 > Time.deltaTime); }

From what I understand, this code example does not do 50% interpolation per second.

How can I do a Linear Interpolation that is dependent on time (not frame rate) and is 50%?

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 meat5000 ♦ · Mar 19, 2018 at 12:52 AM 0
Share

Its in the $$anonymous$$anual! And answered extensively.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Mar 19, 2018 at 12:57 AM

Your use of lerp is not a linear interpolation over time. Time.deltaTime can only be used to compensate linear processes. Taking 50% of the remaining is not a linear process but an exponential process. This can not be compensated with a linear factor as each iteration has a different effect on the outcome. Linear functions have a constant derivative while exponential functions usually have an exponential derivative.


To actually calculate the factor needed every frame to achieve a 50% drop per second you would need to take the "n-th" root of "0.5" where n is the current framerate. For example at a framerate of 60 frames per second you would calculate the "60th" root of 0.5 which is about "0.988514". If you multiply a value by this factor 60 times you effectively have multiplied it by 0.5. However calculating the "1/deltaTime"th root of your desired factor is a quite expensive operation. Specifically using Mathf.Pow(0.5f, Time.deltaTime);


The example given on the page you linked is actually a really bad example usage for lerp. If you want a gradually slowed down interpolation from the current value towards a target you want to use Mathf.SmoothDamp just like they mention at the very bottom of the page you linked.

 private float speed = 0;
 void Update()
 {
     light.intensity = Mathf.SmoothDamp(light.intensity, 8f, ref speed, 10f);
 }

Note that this will bring the intensity to the value of "8" within 10 seconds. Also note that the speed variable is controlled by the SmoothDamp function and is used to save the state of the velocity between frames. You shouldn't set this value manually. Just make sure you have an actual variable that persists between two frames.

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 GambaDeveloper · May 02, 2021 at 02:59 AM 0
Share

What a hell of a function that is! will come very handy

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

82 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 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

Multiple Lerp Calculations 0 Answers

The difference between Time.time and Time.deltaTime 2 Answers

Healthbar color lerp between green, yellow, and red 4 Answers

How to add force in random directions? 0 Answers

Movement for GameObject[] in arrays jittery when using Lerp in Coroutine 0 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