Search Unity

Advantage of using Slerp?

Discussion in 'Scripting' started by AndorfTheWhite, May 22, 2019.

  1. AndorfTheWhite

    AndorfTheWhite

    Joined:
    Nov 1, 2015
    Posts:
    31
    Hi everyone,

    I was wondering what is the advantage of using Slerp for moving or rotating gameobjects instead of transform.translate or physics based (rigidbody) movements. I am guessing it's because you can directly pass the beginning and end position all to one method instead of having to check with if statements the cases in which the object has reached its final destination. So mainly practical reasons, but is this correct?

    Thanks
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,467
    advantage?

    They're different...

    Transform.Translate moves an object the amount of change in the pass in vector. You'd use this when you wanted to move an object a linear amount of change. Transform.Translate(v) is pretty much the same as saying Transform.position += v OR Transform.localPosition += v (depending which relativeTo is Self or World)

    Physics Based Rigidbody - it's physics, you'd use it when you want physics

    Slerp - which slerp? Vector3.Slerp, Quaternion.Slerp... I'm assuming Vector3.Slerp. This doesn't move transforms... it only returns a Vector. You could pass that resulting vector to 'Translate' if you desired, you could set a transform's position with it, you could do any number of things with the resulting vector. Mind you though, Slerp is spherical in nature (as opposed to linear). Further dividing it from Transform.Translate.

    So advantages?

    None...

    It's like asking what's the advantage of drinking water vs riding a bicycle. They don't really have anything to do with each directly other than that they both are healthy activities, and you probably want to drink water when riding a bike.
     
    SparrowsNest likes this.
  3. AndorfTheWhite

    AndorfTheWhite

    Joined:
    Nov 1, 2015
    Posts:
    31
    I mean in the context of moving an object from pos1 to pos2, for example. I see most people using Vector3.Slerp instead of the other logics. You could hypothetically use all these logics to move the object..
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,467
    Slerp is spherical.

    Translate is not.

    So there's no real advantage over the 2... they behave distinctly differently.

    Do you mean Lerp and Translate?
     
  5. AndorfTheWhite

    AndorfTheWhite

    Joined:
    Nov 1, 2015
    Posts:
    31
    Sorry, my bad.. I wanted to say Lerp!
     
  6. AndorfTheWhite

    AndorfTheWhite

    Joined:
    Nov 1, 2015
    Posts:
    31
    So, yes the question persists, now the correct one though: would Lerp be advantageous for moving objects from A to B?
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,467
    Adventageous?

    Depends on your desired result.

    Translate moves an object by some direction/magnitude.

    Code (csharp):
    1. transform.Translate(new Vector3(1f, 0f, 0f))
    This moves the object 1 unit along its x-axis.

    Code (csharp):
    1. transform.localPosition += new Vector3(1f, 0f, 0f);
    This effectively does the same thing.

    ...

    Vector3.Lerp on the other hand returns a Vector3 at some position relative to the 2 vectors passed in.

    It's the formula: f(a, b, t) = (b - a) * t + a
    where t is clamped from 0->1 (unless you use LerpUnclamped)

    So if you say:
    Code (csharp):
    1. transform.position = Vector3.Lerp(transform.position, target, 0.5f);
    The transform is placed half way between its current position and target.

    The difference isn't adventageous... it's just DIFFERENT.

    They do 2 different things.

    Case in point you could do:
    Code (csharp):
    1. transform.translate((target - transform.position) * 0.5f, Space.World);
    And it's the same as my previous Vector3.Lerp code.

    ...

    Now if you have a SPECIFIC usage of Vector3.Lerp vs Translate that you'd like to compare... sure, we can do that. Share your code and we'll look at it.

    But as functions themselves... they're just different.
     
    SparrowsNest likes this.
  8. AndorfTheWhite

    AndorfTheWhite

    Joined:
    Nov 1, 2015
    Posts:
    31
    Ahh okay I see. I was watching a tutorial on how to program interactable objects and the instructor was essentially using Vector3.Lerp in a while loop, combined with Time.DeltaTime and curves to bring the object from point A to B while smoothing its motion. I understand what you mean, Vector3.Lerp by itself just returns some interpolated position between start and end.

    Thank you!
     
  9. AndorfTheWhite

    AndorfTheWhite

    Joined:
    Nov 1, 2015
    Posts:
    31
    So in the case which I specified (moving an object in a certain time frame) would using Lerp be better? Maybe now we got to the right question
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,467
    Better than what?

    Do you have a version of what you describe that DOESN'T use Vector3.Lerp, yet accomplishes the identical behaviour?
     
  11. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,224
    In case you didn't know, LERP is actually short for Linear intERPolation, so that's all it does. It's nothing but a simple mathematical formula to find a value that's part way between two values.
     
  12. AndorfTheWhite

    AndorfTheWhite

    Joined:
    Nov 1, 2015
    Posts:
    31

    Yes, I had used this code to accomplish a door opening and closing based on player input (from another script):

    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.  
    4.         if (apertura == true)
    5.         {
    6.             if (transform.localPosition.z > -2)
    7.             {
    8.                 transform.Translate(new Vector3(-MoltiplticatoreVelocità, 0, 0)); //new Vector3(0, 0, Time.deltaTime * MoltiplticatoreVelocità);
    9.             }
    10.             else
    11.             {
    12.                 apertura = false;
    13.             }
    14.         }
    15.  
    16.         if (chiusura == true)
    17.         {
    18.             if (transform.localPosition.z <= 0)
    19.             {
    20.                 transform.Translate(new Vector3(MoltiplticatoreVelocità, 0, 0));
    21.             }
    22.             else
    23.             {
    24.                 chiusura = false;
    25.             }
    26.         }
    27.     }
    28.  
    29.     public void PulsanteApri()
    30.     {
    31.         chiusura = false;
    32.         apertura = true;
    33.     }
    34.  
    35.     public void PulsanteChiudi()
    36.     {
    37.         apertura = false;
    38.         chiusura = true;
    39.     }
    40. }

    Probably very inefficient since this script was attached to a door which was equipped with a collider, but that's another story. In general, this could easily accomplish opening and closing of the door.
     
  13. AndorfTheWhite

    AndorfTheWhite

    Joined:
    Nov 1, 2015
    Posts:
    31
    Thanks, didn't know the origin of the method name. I understand how linear interpolation works, it's just that I am trying to figure out why one would use it coupled with other code to move objects instead of a transform.Translate.
     
  14. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,176
    Well Slerp would be inappropriate for that use case because it would not be linear, as that code would be, so it's doing a different thing. Lerp would be a little better largely because it'd be easier to ensure that it settled in the correct final position. That said, you should probably be animating doors opening and closing with an animator, which are easier to set up, tweak, and customize.
     
  15. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,224
    Translate requires a "translation" vector, that is to say, it represents the direction and amount that you want to move (relative to your current position). On, the other hand, if you Lerp two positions, you get another position. So you can still use translate, but then you'd have to subtract the original position from the new position to get a translation. Why do that when you can just set the position explicitly and be done with it?
     
  16. AndorfTheWhite

    AndorfTheWhite

    Joined:
    Nov 1, 2015
    Posts:
    31
    Ok thanks everyone. I think overall the main reason for usage is what StarManta mentioned: "Lerp would be a little better largely because it'd be easier to ensure that it settled in the correct final position". Not sure though. I saw a professional programmer using it in a tutorial on game dev on Unity, so I got curious.