• 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
0

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 · 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

81 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

Related Questions

Looking for a turn-based strategy beginner's tutorial. 5 Answers

Tutorial level 1 Answer

2D Rougelike Tutorial-Part 13 -Music Will Not Play 0 Answers

Best waybto make in game tutorial 1 Answer

Tutorial for making a to do list on Unity 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges