Home > Back-end >  2D line renderer reflection issue - Unity
2D line renderer reflection issue - Unity

Time:03-30

I make a 2d line renderer reflection on a specific object tag, it's working but only on the left side when on the right side the reflection is not showing, I don't have any idea why because when my script is on 3d it's working fine.

this is a script that I convert from 3D and I change it all to 2D.

    public int reflections;
    public float maxLength;

    private LineRenderer lineRenderer;
    public LayerMask layerMask;
    private Ray2D ray;
    private RaycastHit2D hit;

    private void Awake()
    {
        lineRenderer = GetComponent<LineRenderer>();
    }

    private void Update()
    {
        ray = new Ray2D(transform.position, transform.up);

        lineRenderer.positionCount = 1;
        lineRenderer.SetPosition(0, transform.position);
        float remainingLength = maxLength;

        for (int i = 0; i < reflections; i  )
        {
            hit = Physics2D.Raycast(ray.origin, ray.direction, remainingLength, layerMask);

            if (hit)
            {
                lineRenderer.positionCount  = 1;
                lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
                remainingLength -= Vector2.Distance(ray.origin, hit.point);
                ray = new Ray2D(hit.point, Vector2.Reflect(ray.direction, hit.normal));
                if (hit.collider.tag != "Reflect")
                    break;
            }
            else
            {
                lineRenderer.positionCount  = 1;
                lineRenderer.SetPosition(lineRenderer.positionCount - 1, ray.origin   ray.direction * remainingLength);
            }
        }
    }

PREVIEW

enter image description here

When going to the right.

enter image description here

When going to left.

Sometimes it flickers too, I don't have any idea how this happens, I thought it was because order layer I have changed this but nothing happen.

CodePudding user response:

In regard to the flickering, this is occuring due to a clipping issue with the collided object and the line itself.

Inside of the condition:

if (hit)

Adjust the code to be the following:

// Get the reflected vector of the raycast.
Vector2 updatedDirection = Vector2.Reflect(ray.direction, hit.normal);
                    
// Create new Ray object & set origin to be 0.01f away from hitpoint so the line is not colliding with the gameobject collider.
ray = new Ray2D(hit.point   updatedDirection * 0.01f, updatedDirection);

You can find out more from these links:

https://answers.unity.com/questions/1602542/line-renderer-flickering-when-updated-in-runtime.html

https://answers.unity.com/questions/1690411/help-with-reflecting-in-2d.html?childToView=1690554#comment-1690554

As for the reason the line is not reflecting on the right wall, this is more than likely due to the gameObjects tag not being set to "Reflect". You are only creating a new reflected line when colliding with an object with that tag. Double check that the right walls gameObject has the tag "Reflect" set in the inspector.

  • Related