0

Im stuck on how to lerp between points of the linerenderer which are added on runtime. It should animate in order. So IEnumerator should be used i guess.

  private void makeLine(Transform finalPoint)
    {
        if(lastPoints == null)
        {
            lastPoints = finalPoint;
            points.Add(lastPoints);
        }
        else
        {
            points.Add(finalPoint);
            lr.enabled = true;
            SetupLine();
        }
    }


    private void SetupLine()
    {

        int pointLength = points.Count;
        lr.positionCount = pointLength;

        for (int i = 0; i < pointLength; i++)
        {
            lr.SetPosition(i, points[i].position);
            // StartCoroutine(AnimateLine());
        }
    }

I found a code example. But now sure how to implement it so it would fit the code above:

    private IEnumerator AnimateLine()
    {
        //coroutinIsDone = false;
        float segmentDuration = animationDuration / points.Count;
        for (int i = 0; i < points.Count - 1; i++)
        {
            float startTime = Time.time;

            Vector3 startPosition = points[i].position;
            Vector3 endPosition = points[i + 1].position;

            Vector3 pos = startPosition;
            while (pos != endPosition)
            {
                float t = (Time.time - startTime) / segmentDuration;
                pos = Vector3.Lerp(startPosition, endPosition, t);

                for (int j = i + 1; j < points.Count; j++)
                    lr.SetPosition(j, pos);

                yield return null;
                
            }
        }
    }
aokde
  • 11
  • 1

0 Answers0