Skip to main content
This is C# code, not the deprecated language UnityScript.
Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373
Source Link

Lerp Teleporting In Unity

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());
}    

}