- Home /
 
               Question by 
               Connerscraft · Dec 18, 2016 at 10:35 PM · 
                2dupdatelinerendererlineupdate function  
              
 
              Update LineRenderer every frame (Delete Old Lines)
Hello,
I am trying to write a script to draw a line from a point to the position of a touch to the screen. I have been able to do this, but I cannot figure out how to make the lines from the previous frames disappear. Any help as to how to go about doing this would be greatly appreciated! My current code is below. Thank you!
  void Update () {
         if (Input.GetMouseButton (0)) {
         drawToMouse (Vector3.zero);  //as a test, drawing from center to mouse
             }
     }
     
     //Draw a line from start to touched position
     private void drawToMouse(Vector3 start) {
             LineRenderer line = Instantiate (linePrototype) as LineRenderer;
             Vector3 pointB = inputCamera.ScreenToWorldPoint (Input.mousePosition);
             start = inputCamera.ScreenToWorldPoint (start);
             line.SetVertexCount (2);
             line.SetPosition (0, start);
             line.SetPosition (1, pointB);
     }
 
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by SunnyChow · Dec 19, 2016 at 02:39 AM
  LineRenderer line;
 void Update () {
          if (Input.GetMouseButton (0)) {
          drawToMouse (Vector3.zero);  //as a test, drawing from center to mouse
              }
      }
      
      //Draw a line from start to touched position
      private void drawToMouse(Vector3 start) {
          if(line)  Destroy(line);
              line = Instantiate (linePrototype) as LineRenderer;
              Vector3 pointB = inputCamera.ScreenToWorldPoint (Input.mousePosition);
              start = inputCamera.ScreenToWorldPoint (start);
              line.SetVertexCount (2);
              line.SetPosition (0, start);
              line.SetPosition (1, pointB);
      }
But the point is... You shouldn't destroy your LineRenderer and recreate one in every frame. You should create your LineRenderer in the beginning, and in every frame only change the vertex positions
Thank you for your suggestion. For some reason it was not working properly when I first had tried to just update the vertex. Got it working now. :)
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                