Search Unity

how exactly does vector3.lerp,mathf.lerp work

Discussion in 'Scripting' started by simonlvschal, Sep 25, 2017.

Thread Status:
Not open for further replies.
  1. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    i have found a couple of problems when using vector3.lerp or mathf.lerp and the problem is when i use either of those functions i will not get the entier value back like

    Code (CSharp):
    1. Vector3.Lerp(0,3,0.0225f);
    2.  
    i do know Lerp is from 0 to 1 and return the value depending on that like 1 means 100% the exact value. while 0.5 is the half and so on.

    now my question is how do i return the true value depending on my travel time? like i want a nice smooth transition but i can't seem to find a way to do so and i rather not be Frame depended
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    If your moving some thing from one point to another Vector3.MoveTowards or even Vector3.SmoothDamp may work a little better in your situation.

    To answer your question. If it takes 5 seconds to travel from point A to point B, then we have to have a counter to store that time and use it to calculate the normalized time to feed into the lerp function.

    Code (CSharp):
    1. float timePassed = 0;
    2. float timeToMove = 5;
    3.  
    4. Vector3 pointA = new Vector3(0, 0, 0);
    5. Vector3 pointB = new Vector3(10, 0, 0);
    6.  
    7. void Update()
    8. {
    9.     if (pointA != pointB)
    10.     {
    11.         timePassed += Time.deltaTime;
    12.  
    13.         float normalizedTime = timePassed / timeToMove;
    14.         transform.position = Vector3.Lerp(pointA, pointB, normalizedTime);
    15.     }
    16.    
    17. }
     
    Bunny83 and JoeStrout like this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Bunny83 and KelsoMRK like this.
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I think the if-statement is probably fine, though probably also unnecessary. Unity's Lerp functions clamp the 't' parameter to the range [0,1], so after normalizedTime >= 1, you will always be getting back simply pointB.
     
    Bunny83 likes this.
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    And Vector3 equality operators use approximately close. In either case, it's probably sufficient to check if timePassed < timeToMove anyway.
     
    JoeStrout likes this.
  6. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Baste means that the if statement is supposed to be if (transform.position != pointB). Point A will never be Point B since Point A never changes.
     
    Bunny83 and KelsoMRK like this.
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Good point — though the intent might have been to not bother with the Lerp if the start and endpoints were the same.

    It'll work regardless — Lerps are cheap (and clamped).
     
  8. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Oops, wrote that too quickly. At least the idea is there.
     
  9. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
  10. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    well i found a solution unity's own document for vector3.lerp is actually updated and works nicely.
     
  11. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    since you asked exactly how it works

    Code (CSharp):
    1.     public static float Lerp(float a, float b, float t)
    2.     {
    3.       return a + (b - a) * Mathf.Clamp01(t);
    4.     }
    Code (CSharp):
    1.     public static Vector3 Lerp(Vector3 a, Vector3 b, float t)
    2.     {
    3.       t = Mathf.Clamp01(t);
    4.       return new Vector3(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t);
    5.     }
     
    lordofduct likes this.
  12. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    when it checks equality on 2 vector3's it actually does not check each component but subtracts the 2 vectors, then checks to see if the SqrMagnitude is smaller then a very small number
     
  13. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I never said it did :)
     
  14. madeforgamingamk

    madeforgamingamk

    Joined:
    Apr 5, 2023
    Posts:
    3
    the main difference between them is that Vector3.Lerp takes three parameters two vectors and one float interpolation value and mathf.Lerp takes three parameters of float type
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Please don't necropost. What you posted is in the Scripting docs already and the actual code was posted above in 2017!

    https://github.com/Unity-Technologi...98f64f5fb8/Runtime/Export/Math/Vector3.cs#L38

    https://github.com/Unity-Technologi...398f64f5fb8/Runtime/Export/Math/Mathf.cs#L222

    Thanks.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.