Search Unity

There are some big issues with Unity's Lerp tutorial. Who can I talk to about it?

Discussion in 'Editor & General Support' started by Oana, Jan 16, 2015.

  1. Oana

    Oana

    Joined:
    Sep 28, 2012
    Posts:
    87
    I never watched the scripting tutorials previously, being a programmer who had been working with Unity long before they were up, but it has recently come to my attention (from Reddit) that the Lerp tutorial advises some really bad practices, and I don't really know who to contact about it, so I'm posting here.

    What's wrong with it? It advises people to do stuff like:

    Code (csharp):
    1. transform.position = Vector3.Lerp(transform.position,destination,Time.deltaTime);
    Why is this an issue? It can cause some really nasty bugs.

    Posting here my explanation from Reddit on why this is wrong:
    If I am wrong about this in any way, please let me know how and why. If I am not wrong, then how can we go about changing the tutorial so that newbies stop being mislead?
     
    iamcastelli likes this.
  2. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    I think the point of the example is just to illustrate using lerp.

    Dont rely on lerp to get to the destination.
    Put a test in so that if some threshold is met then set to the destination and stop lerping....
     
  3. Oana

    Oana

    Joined:
    Sep 28, 2012
    Posts:
    87
    The point is, you can use Lerp to ensure you reach the destination just fine, and the 3rd parameter is supposed to be a value between 0 and 1, not Time.deltaTime. See the actual API docs: http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

    Using it with Time.deltaTime as a parameter is NOT its intended usage, from what I can tell. Am I wrong in this?
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Lerp with delta time is fine for movement, just don't use it to check if arrived. To check if arrived would require using a variable there and incrementing the variable until >= 1.

    Yes you are wrong in this. It's not a flaw but by design for this example. It's not broken and isn't wrong for this use case.

    Otherwise it'd be called LerpGuaranteedDestination or some bizarre thing like that. Lerp can be used in a number of different ways, and all of them are perfectly valid uses. What you put in t is up to you. The Learn example never proposes to demonstrate reaching t = 1.

    So in reality, it's your issue since you're doing something different, and the use of lerp depends on the individual.

    How are they being misled? What would you like to see added?
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That particular method isn't wrong, technically, and is occasionally useful, but it does mislead newbies. It's sort of a hack, in a sense. I frequently see questions where people think Lerp is some kind of magic function that moves things over time, instead of what it is (just a stupidly simple math function that returns a value immediately). The use of Time.deltaTime as the third parameter would seem to encourage that view.

    --Eric
     
    Good_Rabbi and Ony like this.
  6. Oana

    Oana

    Joined:
    Sep 28, 2012
    Posts:
    87
    Thanks for all of your replies. I guess maybe calling it "wrong" is a bit too extreme. I guess the right question to ask is: should we introduce the dirty hack aspect of "Lerp" in the "Lerp" tutorial rather than first show how to use it as described by the API?

    It is used as a way to get from point A to B in the tutorial, with no explanation of the fact that it doesn't actually take you to B. It also does not make clear what the "t" parameter is, it shows no example of using it with t having an incremental value between 0 and 1 and thus no proper explanation of what it actually does. As Eric5h5 mentions, newbies will only know it of being "some kind of magic function that moves things over time", which is what the tutorial kind of show it as.
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Alright - I'll post this thread to the learn team and it will be up to them to clarify in future.

    Lerp under the hood probably does something similar to:


    Code (CSharp):
    1.     public static float Lerp(float start, float end, float value)
    2.     {
    3.         return ((1.0f - value) * start) + (value * end);
    4.     }
     
    Oana likes this.
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    To be precise, it's

    Code (csharp):
    1. public static float Lerp (float from, float to, float t)
    2. {
    3.    return from + (to - from) * Mathf.Clamp01 (t);
    4. }
    Some years ago there was a discussion about optimizing some of these math functions; you can get better performance by using your own code instead (by eliminating the extra function call, and early outs for clamped values):

    Code (csharp):
    1.     public static float Lerp (float from, float to, float t) {
    2.         if (t <= 0.0f)
    3.             return from;
    4.         else if (t >= 1.0f)
    5.             return to;
    6.         return from + (to - from) * t;
    7.     }
    But sadly nothing was ever done.

    --Eric
     
  9. Oana

    Oana

    Joined:
    Sep 28, 2012
    Posts:
    87
    Having THAT in the tutorial would be awesome! :D I pretty much assumed that was how it worked in the background, but then again I had to make it manually a gazillion times before I started using Unity. More and more people unfortunately start learning how to program directly by using Unity, copypasting scripts and trying to tweak them to get them to work rather than actually trying to understand what's going on.

    Many thanks for listening to my little rant and concerns.
     
  10. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It's all good feedback.
     
    Oana likes this.
  11. BIG-BUG

    BIG-BUG

    Joined:
    Mar 29, 2009
    Posts:
    457
    Furthermore the tutorial's short description is not just misleading, it's wrong:
    "How to use Lerp to ‘Linearly Interpolate’ (smoothly transition) between values for various data types."
    Correct:
    "How to use Lerp (Linearly Interpolate) to transition smoothly between values for various data types."
     
  12. JamesB

    JamesB

    Unity Technologies

    Joined:
    Feb 21, 2012
    Posts:
    133
    Hey guys,

    @Oana, you make some good points about the tutorial. The intended usage of Lerp isn't just to move objects as you correctly point out.

    The intention of the tutorial is to show an example of usage. I do concede that this is probably one of my worst tutorials and I would love to have the time to redo it, the actual use of Lerp does get lost in the example. I will do my best to find the time to remake this tutorial but unfortunately I am very busy at the moment.

    Thank you for your feedback, all feedback is always welcome. It is definitely noted and I will do my best to act on it as soon as I can. Unfortunately I don't know when that will be.

    James
     
    Oana likes this.
  13. Oana

    Oana

    Joined:
    Sep 28, 2012
    Posts:
    87
    Thanks for listening @JamesB! I think you should definitely leave the tutorial there on the long run, as an example, but call it something else and make a separate lerp tutorial.

    I do understand the lack of time though... I've been meaning to do some tutorials myself but I keep getting caught up in other projects...
     
  14. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    As the tutorial covers three uses of Lerp, I don't think it's misleading. It could perhaps be explained with more detail (haven't watched vid so don't know), but it's certainly not presenting Lerp as just a means to move things when it's also changing light and colours. The real problem with the given use is, as a linear interpolation, we're actually getting a non-linear movement because the 'from' value is changing. That's not the intention of a linear interpolation, but in (games) coding you are free to use whatever solution works for you (as long as it works!). An ideal example would be to lerp transform.position (or colour, etc.) between Start and End values.

    The fundamental idea of the lerp is that it's a blend between two values, returning a percentage mix of the two, and the tute misses this.
     
  15. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    Indeed! I dont understand the obsession for using time, while talking about linear interpolation. Nowadays the docs seems to be much better. Last time i checked it was Lerp(x,y,Time.time) :)

    Third value t in
    public static Vector3 Lerp(Vector3 from, Vector3 to, float t);

    should be p

    Just drop all time things from documentation example, because its only confusing.
     
  16. JamesB

    JamesB

    Unity Technologies

    Joined:
    Feb 21, 2012
    Posts:
    133
  17. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Hey James, I didn't require it but I had a look. I must say there is slight comedy value here in how far you went. You left nothing to chance and it's a great job.

    Now for the other 9999 things ... :)
     
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    This is good...but.... There's always a but. ;) You're still using this method:

    Code (csharp):
    1. light.intensity = Mathf.Lerp(light.intensity, 8f, 0.5f * Time.deltaTime);
    I retract what I said earlier about this being "occasionally useful". It is in fact wrong (Oana was right from the start) and framerate-dependent, and it would be best to discourage this from being used at all. Consider if the light.intensity started at 0.0 and you were getting 100fps. After 2 seconds (200 iterations), it would be at 5.064338. Now let's say you're getting 10fps. After 2 seconds (20 iterations), the value would be 5.132113. It's not an incredibly vast difference in this case, but the point is that different framerates do get measurably different results. It's not hard to think of scenarios where this could present more serious problems.

    --Eric
     
    reinfeldx and hippocoder like this.
  19. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    I remember this coming up a few years ago - I posted a few replies here: http://forum.unity3d.com/threads/ho...ithout-causing-jitter-in-the-movement.130920/

    You should never pass a linear multiple of delta time as the lerp argument, it doesn't make sense, it is simple but mathematically always wrong. Assuming you want the behaviour of moving swiftly towards a point but slowing down on the approach, and you want correct compensation for delta time, you ought to use 1 - exp(-k * dt) as the lerp argument, and tweak k to control the rate.

    That old thread also includes some more advanced stuff for smoothly lerping towards a moving target with correct delta time compensation, but that's another story.
     
    wikmanyo likes this.