Search Unity

Bug Moving Object from left to right, pause for a couple of seconds at right, then move back left.

Discussion in 'Scripting' started by SAOGonza, Feb 1, 2023.

  1. SAOGonza

    SAOGonza

    Joined:
    Jun 5, 2021
    Posts:
    1
    I'm trying to start an object at it's currPosition (12, 9.15, 45) and bring it to the goal position (12, 9.15, 57), pause for 2 seconds, then bring it back to currPosition. I'm trying to loop it infinitely.
    The code works for the most part. It stops for 2 seconds, but instead of pausing then resuming, it pauses, but once it resumes, it instantly teleports to where it should've been if it never paused in the first place.

    What I'm doing: Making a spotlight move left and right. If player touches light, respawn to Vector3(0,0,0), hence the name "LightLerp".

    Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LightLerp : MonoBehaviour
    6. {
    7.     [SerializeField] private Vector3 currPosition;
    8.     [SerializeField] private Vector3 goalPosition;
    9.     [SerializeField] private float speed = 0.5f;
    10.     private float time, target = 1;
    11.     [SerializeField] private bool isWaiting = false;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.      
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         // Variables
    23.         time = Mathf.PingPong(Time.time * speed, target);
    24.  
    25.         if (!isWaiting)
    26.         {
    27.             transform.position = Vector3.Lerp(currPosition, goalPosition, time);
    28.  
    29.             // Pause the light from moving once we reached the edge.
    30.             if (Vector3.Distance(currPosition, transform.position) < 0.1f || Vector3.Distance(goalPosition, transform.position) < 0.1f)
    31.             {
    32.                 StartCoroutine(Move());
    33.             }
    34.         }
    35.     }
    36.  
    37.     IEnumerator Move()
    38.     {
    39.         isWaiting = true;
    40.         Debug.Log("Waiting");
    41.         yield return new WaitForSeconds(2);
    42.         Debug.Log("Continue");
    43.         isWaiting = false;
    44.     }
    45. }
    46.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Can you just use an animation and delete all the above code?

    Otherwise sounds like the PERFECT use of a tweener, like LeanTween / DOTween / iTween.

    Why reinvent the wheel?

    But if you absolutely MUST do it in code yourself, here's a handy pattern:

    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