Get Line Renderer to restart from start everytime using coroutine. HELP?!?!?!?

So I have a player that uses a line renderer animation to help with the trajectory.

The line render animates from one point to another using two game object positions.

The animation works perfectly the first time, however when re clicked the line renderer animation doesn’t reset. It just stays at full length.

Any ideas on how to reset the position so the animation starts every time the mouse is clicked?

Any help would be greatly appreciated :slight_smile:

LineRenderer link;
public Transform[] pos;

public float speed;

bool assist; 


public void Update()
{ 

    
   

    if (Input.GetMouseButton (0)) 
     {
         
         StartCoroutine(Assist());
         assist = true;
         
    
     }

     if (Input.GetMouseButtonUp (0)) 
     {
         
         StartCoroutine(Assist());
         assist = false;
         
    
     }

   
}

public IEnumerator Assist() 
{

    link = gameObject.GetComponent<LineRenderer> ();

   if (assist == true) 
   {

         yield return null;

         link.enabled = true;
         link.SetPosition(0, pos[0].position);
         link.SetPosition(1, Vector3.MoveTowards (transform.position,pos[1].position,speed *Time.time));
         
    }

    if (assist == false)
    {
        yield return null;
        
        link.enabled = false;

    }


}  

}

@harrylongman11 Hope this helps. Consider using [SerializeField] instead of public if your intention is just to see and change the value in the inspector.

    LineRenderer link;
    public Transform[] pos;
    public float speed = 2; //A speed of 2 is a good starting point
    bool assist;

    Vector3[] linkPos = new Vector3[2]; //Array containing two vector3

    void Awake()
    {
        link = gameObject.GetComponent<LineRenderer>(); //We get components in Awake so that it only happens once
    }
    public void Update()
    {
        CheckInput(); //Try to keep your update functions neat by making them point to other functions
        Assist();
    }

    void CheckInput()
    {
        assist = Input.GetMouseButton(0); //We assign the assist boolean to become our getmousebutton 0
    }

    void Assist()
    {
        link.enabled = assist;
        if (link.enabled)
        {
            linkPos[0] = pos[0].position;
            linkPos[1] = Vector3.Lerp(linkPos[1], pos[1].position, speed * Time.deltaTime); 
            //Using time.deltatime will make your line "animate" according to framerate, which is better in most cases
            //Keep in mind that lerping a vector3 like this every frame might not be efficient, but it works
        }
        else
        {
            //Here we make the laser return home to the starting point, because you let go of the mouse button 0
            linkPos[0] = pos[0].position;
            linkPos[1] = pos[0].position;
        }

        link.SetPosition(0, linkPos[0]);
        link.SetPosition(1, linkPos[1]);
    }