Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question ping pong lerp help

Discussion in 'Scripting' started by melonhead, Apr 1, 2023.

  1. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    592
    i am trying to ping pong a lerp so it moves an object smooth so when hold mouse key it starts to travel to its end point but if release mouse key at any point it smooth travels back to its starting point using the same time duration it used before releasing the mouse button, i am in a bit of a pickle, can anyone help get this to work

    Code (CSharp):
    1. using UnityEngine;
    2. public class LerpUpdate3 : MonoBehaviour
    3. {
    4.     Vector3 start_pos;
    5.     Vector3 end_pos;
    6.     Vector3 ofset= new Vector3(5,0,0);
    7.     Vector3 current_pos;
    8.  
    9.     public float timer;
    10.  
    11.     public float duration;
    12.  
    13.  
    14.  
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         start_pos=transform.position;
    20.         current_pos= transform.position;
    21.         end_pos=transform.position+ofset;
    22.        
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         if (Input.GetKey(KeyCode.Mouse0) && current_pos.x<end_pos.x)
    29.         {
    30.             current_pos.x=Mathf.SmoothStep(start_pos.x,end_pos.x,timer/duration);
    31.             transform.position=current_pos;
    32.             timer+=Time.deltaTime;
    33.  
    34.         }else{
    35.  
    36.             if (Input.GetKeyUp(KeyCode.Mouse0)){
    37.  
    38.                             current_pos.x=Mathf.SmoothStep(current_pos.x,start_pos.x,timer/duration);
    39.             transform.position=current_pos;
    40.             timer+=Time.deltaTime;
    41.  
    42.  
    43.             }
    44.        
    45.        
    46.        
    47.     }
    48. }
    49. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    33,456
    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

    Another approach would be to use a tweening package line LeanTween, DOTween or iTween.
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,031
    It looks like you are expecting Input.GetKeyUp to return true any time the player is not pressing the mouse button. That's not how it works, though. Input.GetKeyUp is only true on the one frame where a key transitions from being pressed to not being pressed.
     
  4. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    36
    If I got the idea right, you can try this.

    Code (CSharp):
    1. using UnityEngine;
    2. public class LerpUpdate3 : MonoBehaviour
    3. {
    4.     Vector3 start_pos;
    5.     Vector3 end_pos;
    6.     Vector3 ofset = new Vector3(5, 0, 0);
    7.     Vector3 current_pos;
    8.  
    9.     float progress = 0;
    10.  
    11.  
    12.     public float duration;
    13.  
    14.     void Start()
    15.     {
    16.         start_pos = transform.position;
    17.         current_pos = transform.position;
    18.         end_pos = transform.position + ofset;
    19.  
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         float step = (1f / duration) * Time.deltaTime;
    25.  
    26.         if (Input.GetKey(KeyCode.Mouse0))
    27.         {
    28.             progress += step;
    29.         }
    30.         else
    31.         {
    32.             progress -= step;
    33.         }
    34.  
    35.         progress = Mathf.Clamp01(progress);
    36.  
    37.         current_pos.x = Mathf.SmoothStep(start_pos.x, end_pos.x, progress);
    38.         transform.position = current_pos;
    39.     }
    40. }
    41.  
     
    Bunny83 likes this.
  5. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    592
    thank so much, was getting in a muddle trying to feed the current position back in as the start position, just recovering from covid, think it has fried my grey matter

    much appreciated all for help given
     
  6. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    592
    think i now have it working as i need thank you
     
    Last edited: Apr 2, 2023