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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Moving an object within a set number of seconds

Discussion in 'Physics' started by igoapp, Apr 30, 2022.

  1. igoapp

    igoapp

    Joined:
    Aug 8, 2021
    Posts:
    20
    Hi.

    Sorry, this is a very simple question, but it drives me mad. I am trying to move an object from one point to another within a set number of seconds. All the sources that I found on-line point to the following code:

    Code (CSharp):
    1. IEnumerator MovePlayerMobile(Vector3 targetPosition, float time)
    2.     {
    3.         _moveAllowed = false;
    4.         float elapsedTime = 0;
    5.    
    6.          while (elapsedTime < time)
    7.           {
    8.                     transform.position = Vector3.Lerp(transform.position, targetPosition, (elapsedTime / time));
    9.                     elapsedTime += Time.deltaTime;
    10.                     yield return null;
    11.           }
    12.           transform.position = targetPosition;
    13.  
    14.         float boundary = 50f;
    15.         if (transform.position.x < -boundary)
    16.         {
    17.             transform.position = new Vector3(-boundary, transform.position.y, transform.position.z);
    18.         }
    19.         else if (transform.position.x > boundary)
    20.         {
    21.             transform.position = new Vector3(boundary, transform.position.y, transform.position.z);
    22.         }
    23.  
    24.         yield return new WaitForSeconds(0.01f);
    25.  
    26.         _moveAllowed = true;
    27.     }
    However, what it really does is moving the object in approximately a second, and then waiting for the remaining time determined by the passed parameter time (the while loop continues by just adding elapsedTime). So, if the time is 5 seconds, for example, the object is being completely moved during 1 second, and then it is holding for 4 seconds. What am I doing wrong?

    Thank you.
     
    Last edited: May 1, 2022
  2. igoapp

    igoapp

    Joined:
    Aug 8, 2021
    Posts:
    20
    Found an answer. The transform.position was constantly updated in the while loop to the current one. I copied it into another variable and passed it instead.

    Code (CSharp):
    1. IEnumerator MovePlayerMobile(Vector3 startPosition, Vector3 targetPosition, float time)
    2.     {
    3.         _moveAllowed = false;
    4.         float elapsedTime = 0;
    5.  
    6.                 while (elapsedTime < time)
    7.                 {
    8.                     transform.position = Vector3.Lerp(startPosition, targetPosition, (elapsedTime / time));
    9.                     elapsedTime += Time.deltaTime;
    10.                     yield return null;
    11.                 }
    12.                 transform.position = targetPosition;
    13.            
    14.         _moveAllowed = true;
    15.     }