- Home /
Line Renderer flickering when updated in runtime
Check this link to see the issue in action. the code:
 using UnityEngine;
 [RequireComponent(typeof(LineRenderer))]
 public class MirrorReflect : MonoBehaviour
 {
     //the attached line renderer
     private LineRenderer lineRenderer;
 
     //the number of reflections
     public int nReflections = 2;
     //max length
     public float maxLength = 100f;
 
     //private int pointCount;
     void Awake()
     {
         //get the attached LineRenderer component  
         lineRenderer = GetComponent<LineRenderer>();
     }
     void Update()
     {
         //clamp the number of reflections between 1 and int capacity  
         nReflections = Mathf.Clamp(nReflections, 1, nReflections);
 
         Ray2D ray = new Ray2D(transform.position, transform.right);
 
         //start with just the origin
         lineRenderer.positionCount = 1;
         lineRenderer.SetPosition(0, transform.position);
         float remainingLength = maxLength;
         //bounce up to n times
         for (int i = 0; i < nReflections; i++)
         {
             RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, remainingLength);
             // ray cast
             if (hit.collider != null)
             {
                 //we hit, update line renderer
                 lineRenderer.positionCount += 1;
                 lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
                 // update remaining length and set up ray for next loop
                 remainingLength -= Vector3.Distance(ray.origin, hit.point);
                 ray = new Ray2D(hit.point, Vector3.Reflect(ray.direction, hit.normal));
                 // break loop if we don't hit a Mirror
                 if (hit.collider.tag != "Mirror")
                     break;
             }
             else
             {
                 // We didn't hit anything, draw line to end of ramainingLength
                 lineRenderer.positionCount += 1;
                 lineRenderer.SetPosition(lineRenderer.positionCount - 1, ray.origin + ray.direction * remainingLength);
                 break;
             }
         }
     }
 }
Help?
Answer by Major_Lag · Jan 17, 2020 at 01:50 AM
Could be wrong but I think when you're reflecting you are sometimes getting trapped the collider you are reflecting off of. Not too long ago i got a similar but not exact problem and the solution was to start your raycast a little bit in the direction of reflection from where you reflected. Perhaps a look at the answer in my question from a few days ago could help you a little. https://answers.unity.com/questions/1690411/help-with-reflecting-in-2d.html?childToView=1690554#comment-1690554
Ahh, I see. thanks, even though I kinda finished this project a while ago, It dosen't hurt to have another correctly working build to be created, right? Thanks though, I finally understand why this was not working!
@jsull1 This is the answer. To fix it with my code up there, i believe you would have to do something like
 Vector2 dir = Vector2.Reflect(ray.direction, hit.normal);
and ins$$anonymous$$d of
 ray = new Ray2D(hit.point, Vector3.Reflect(ray.direction, hit.normal));
you do
 ray = new Ray2D(hit.point + dir * 0.01f, dir);
Answer by jsull1 · Jan 17, 2020 at 01:21 AM
Oh man! tell me you found an answer @Spacejet56 , I'm facing the same issue.
Unfortunately, I could not. I actually could get a similar setup working in 3D, but I could not re-create it in 2D. that was quite a while ago, though, and I may be able to come up with something if I try to re-create it now? I'll get back to you on this thread if I do find a solution.
I just realized, I was doing physics calculations in Update(). Try changing it to FixedUpdate(), maybe that would do the trick?
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Only One LineRenderer Renders 0 Answers
Distribute terrain in zones 3 Answers
Draw line from Player to Mouse position 0 Answers
(C#) Collider to collider2d 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                