-1
\$\begingroup\$

I am trying to make a object move back and fourth but its teleporting instead of moving smoothly. This is my code.

void Start()
{
    StartCoroutine(timer1());    
}

IEnumerator timer1()
{
    yield return new WaitForSeconds(3);

    Vector3 a = transform.position;
    Vector3 b = target.position;
    transform.position = Vector3.Lerp(a, b, t);

    StopCoroutine(timer1());
    StartCoroutine(timer2());
}

IEnumerator timer2()
{
    yield return new WaitForSeconds(3);

    Vector3 A = transform.position;
    Vector3 B = target2.position;
    transform.position = Vector3.Lerp(A, B, t);

    StopCoroutine(timer2());
    StartCoroutine(timer1());
}    

}

\$\endgroup\$
1
  • \$\begingroup\$ Looks like you forgot to show us where your variable t is declared and assigned a value. \$\endgroup\$
    – DMGregory
    Commented Feb 5, 2022 at 13:33

2 Answers 2

0
\$\begingroup\$

Wait for 3 seconds means: put the object here; do nothing for 3 seconds; now move it over there. Teleport, if you like.

To get smooth movement you have to update the position of the object every frame. You don't need a coroutine to do that, just update the position in the Update() callback. The Unity docs have plenty of examples eg https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html.

\$\endgroup\$
2
  • \$\begingroup\$ Is there a way I can make it go back and fourth on a loop? \$\endgroup\$
    – JJVS
    Commented Feb 5, 2022 at 13:14
  • \$\begingroup\$ Of course. Just set the position every frame to a value that changes from A to B and back again over a number of frames. \$\endgroup\$
    – david.pfx
    Commented May 8, 2022 at 1:47
0
\$\begingroup\$

You're making the common beginner error of believing that "Lerp" is a tweening function, ie. that you can invoke Lerp once and it will act on an object over a period of time, moving it until it reaches a destination.

Lerp does not do anything like that at all. It's just a math function that accepts some numbers/vectors and returns a different number/vector. Note that you don't even pass it a reference to the thing that should be moved, so there's no way it could do what you're trying to use it to do.

If you want to move something with Lerp, you need to have a loop that runs every frame, calculates a new position for that one frame using Lerp, and moves the object to that position. Something like this:

// Start a coroutine the first frame this script is enabled:
IEnumerator Start() {
   // Loop forever.
   while (true) {
       // Chain execution to the MoveTo coroutine, until it finishes.
       yield return MoveTo(target.position, 3);

       // Once it's done, chain execution to the MoveTo coroutine again,
       // moving to the other target, until the coroutine finishes.
       yield return MoveTo(target2.position, 3);

       // Return to the top of the loop to move back to the first target.
   }
}

// Move toward a destination point over a given time interval.
IEnumerator MoveTo(Vector2 destination, float duration) {
    // Remember where we started from.
    Vector2 start = transform.position;

    // Control how fast we move.
    float speed = 1f/duration;

    // Loop until we progress from 0% all the way to 100%
    // of our duration, storing the current progress in t.
    for (float t = 0; t < 1; t += Time.deltaTime * speed) {
        // Calculate a new in-between point for this frame, and move there.
        transform.position = Vector2.Lerp(start, destination, t);

        // Wait until next frame before resuming the loop.
        yield return null;
    }

    // Finish exactly at the destination (the last loop execution
    // ran when t was still slightly less than 100% of the way there).
    transform.position = destination;

    // No need to call StopCoroutine, we can just hit the end of the function.
}

Examples like this are everywhere - I've written several myself on this site alone. So please do a bit more research in future to make use of the wealth of Unity code examples that are out there. If you do, you'll find you're often able to fix your own problems, faster.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .