Search Unity

Linear Interpolation - Outdated / Error

Discussion in 'Community Learning & Teaching' started by Zacamunga, Mar 31, 2018.

  1. Zacamunga

    Zacamunga

    Joined:
    Mar 25, 2018
    Posts:
    6
    Hello Friends,

    As I'm following the beginner C# scripting tutorials, I've run into the following issues:
    • The code being taught for light intensity (light.intensity) is obsolete at this point and my attempt at remedying the issue, based on the Unity Scripting API documentation, has led to an error that I'm still trying to figure out:
    Here's the .cs file that's attached to a Point Light with the Point Light object set as lt (see screenshot below).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. /* Lerp - Linear Interpolation
    7. * Allows for a smooth transition between values
    8. * Lerp below will lerp 3 properties of the light: position, color, intensity.
    9. * */
    10.  
    11. public class LerpLight : MonoBehaviour {
    12.  
    13.     public float smooth = 2;
    14.     public Light lt;
    15.  
    16.     private float newIntensity;
    17.  
    18.     void Awake(){
    19.  
    20.         // Get light component
    21.         lt = GetComponent<Light>();
    22.         // Sets current intensity of the object to newIntensity
    23.         newIntensity = lt.intensity;
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update () {
    28.         IntensityChanging ();
    29.     }
    30.      
    31.  
    32.     void IntensityChanging(){
    33.         float intensityA = 0.5f;
    34.         float intensityB = 5f;
    35.  
    36.         if (Input.GetKeyDown (KeyCode.A))
    37.             newIntensity = intensityA;
    38.         if (Input.GetKeyDown (KeyCode.D))
    39.             newIntensity = intensityB;
    40.  
    41.         // Mathf.Lerp(From intensity, To intensity, Distance between From and To * Time it takes to get there)
    42.         lt.intensity = Mathf.Lerp (lt.intensity, newIntensity, Time.deltaTime * smooth);
    43.     }
    44. }
    • The Linear Interpolation video lesson, in general, has some negative reviews saying it's incorrectly using the Lerp code. I certainly don't know any better so had I not run into the first issue, I wouldn't have noticed the comments further explaining the discrepancies. It looks like Time.deltaTime doesn't really 'belong' in the third Lerp property which is reflected in the partially updated written lesson.
      • Is using Time.delta.Time within Lerp inefficient and/or bad practice? It seems like the destination is never reached and it's left constantly updating.
    Any insight is appreciated!

    Thanks,
    Zach
     

    Attached Files:

    Last edited: Mar 31, 2018
  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    That code is fine. Use of Lerp that way is common. You'll find plenty of examples like that, with comments on how it works and why it's fine. The comments that I looked at on that U-Tube page are uninformed junk. Writing that Lerp must be linear is like writing "a + -2" is an error since addition is for making things larger.