Search Unity

Ease out movement for object

Discussion in 'Scripting' started by Jnick1101, Feb 10, 2016.

  1. Jnick1101

    Jnick1101

    Joined:
    Jan 28, 2016
    Posts:
    4
    Hello, Unity Forums! I recently read a tutorial about linear interpolation and decided to use it in my script. I am attempting to make a sphere move around with my arrow keys, but I do not want an abrupt stop motion. When I let go of the key, I want the sphere to ease out slowly, coming to a complete stop. This is currently my script:
    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if (Input.GetKey (KeyCode.UpArrow)) {
    4.  
    5.             transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
    6.  
    7.             if (Input.GetKeyUp (KeyCode.UpArrow))
    8.                 transform.Translate = Vector3.Lerp(transform.position.z, transform.position.z * 10f, 0.5f);
    9.       }
    I am getting 3 errors:

    error CS1502: The best overloaded method match for `UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments

    error CS1503: Argument `#1' cannot convert `float' expression to type `UnityEngine.Vector3'

    error CS0131:The left-hand side of an assignment must be a variable, a property or an indexer


    I don't know if I should use Vector3.Lerp or Mathf.Lerp, or if I'm just not achieving my goal with the appropriate function. The errors aren't helping, either.

    Could someone help me with this problem(s) please?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Vector3.Lerp lerps vectors, Mathf.Lerp lerps floats.

    position.z is a float

    Which do you think you'd use?
     
    GroZZleR likes this.
  3. Jnick1101

    Jnick1101

    Joined:
    Jan 28, 2016
    Posts:
    4
    Okay, I changed it from Vector3.Lerp to Mathf.Lerp, and only have one error:

    error CS0131:The left-hand side of an assignment must be a variable, a property or an indexer

    What do I do now?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    transform.Translate is a function, you're attempting to set a float to a function.
     
  5. Jnick1101

    Jnick1101

    Joined:
    Jan 28, 2016
    Posts:
    4
    What can I do to fix it?
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    What are you even attempting to do?

    Because this code is just moving in a direction while 'UpArrow' is held, and then on the one frame that it's released, you lerp the z position half of 10 units forward. But you attempt to set that to Translate...

    You mention that you don't want abrupt stopping. This code is NOT how you do that.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    You could do something like this:

    Code (csharp):
    1.  
    2. private Vector3 _move;
    3.  
    4. void Update()
    5. {
    6.     if(Input.GetKey(KeyCode.UpArrow))
    7.     {
    8.         _move = Vector3.forward * moveSpeed;
    9.         transform.Translate(_move * Time.deltaTime);
    10.     }
    11.     else if(_move.sqrMagnitude > 0f)
    12.     {
    13.         //ease out
    14.         _move *= 0.5f;
    15.         transform.Translate(_move * Time.deltaTime);
    16.         //once we've reached a slow enough speed, zero out
    17.         if(_move.magnitude < 0.01f) _move = Vector3.zero;
    18.     }
    19. }
    20.  
     
  8. Jnick1101

    Jnick1101

    Joined:
    Jan 28, 2016
    Posts:
    4
    Dude, thanks so much! I have to go over the code a few times to understand it better (I'm starting with Unity again), but you solved my problem!

    P.S. One question: How can I increase the ease out time?
     
  9. walrus_gumboot

    walrus_gumboot

    Joined:
    Dec 31, 2019
    Posts:
    3
    I know it's like four years ago, but why not, right?
    This code halves the movement speed each frame. If you want to make it ease out slower, multiply by a higher number but not higher than one (that would keep accelerating the object), something like
    0.75f
    . The opposite applies too, but make sure you don't go below zero.
     
  10. rumcan

    rumcan

    Joined:
    Apr 2, 2021
    Posts:
    1