Can't produce lines using Line Renderer.

I’m currently working on a 2D bubble shooter like game. Where I want an aim line to help player aim. I’m using Line Renderer to do so. Following is the code I’m using to set positions:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testAimLine : MonoBehaviour
{
    public GameObject obj;
    LineRenderer lr;

    [SerializeField]
    public int numberOfReglections;

    Vector3[] pos = new Vector3[5];
    Vector3[] rot = new Vector3[5];

    private void Start()
    {
        lr = GetComponent<LineRenderer>();

        pos[0] = gameObject.transform.position;
        rot[0] = gameObject.transform.eulerAngles;

        lr.SetPosition(0, pos[0]);
    }
   
    private void Update()
    {
        for (int i = 1; i < numberOfReglections; i++)
        {
            RaycastHit2D ray = Physics2D.Raycast(pos[i - 1], rot[i - 1]);

            pos[i] = ray.point;
            rot[i] = Vector3.Reflect(pos[i], ray.normal);

            lr.SetPosition(i, pos[i]);
        }
    }
}

But due to some unknown reasons it is not working. I’m almost new to coding so be easy on me :wink:

if you start your index from 1, try yo change i <= numberOfReglections

When you use LineRenderer, if you want to display it on some canvas, change the “Render Mode” of the Canvas in “Screen Space - Camera”

Change this also in: pos = ray.point + Vector3.forward * 1;
[quote=“NidoAnxari, post:1, topic: 671794, username:NidoAnxari”]
lr.SetPosition(0, pos[0]);
*[/quote]
*
and this: lr.SetPosition(0, pos[0] + Vector3.forward * 1);
If this all dont work, assing a new material to the line renderer, and change the shader in Sprite/Default

Is there any special reason why you multiply a forward vector by one? Wouldn’t that result in the same thing?

This is the simplified prototype script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testAimLine_1 : MonoBehaviour
{
    public GameObject testObj;

    private void Update()
    {
        RaycastHit2D hit0 = Physics2D.Raycast(transform.position, transform.up);
        Vector2 reflection = Vector2.Reflect(hit0.point, hit0.normal);

        RaycastHit2D hit1 = Physics2D.Raycast(hit0.point, reflection);

        testObj.transform.position = hit1.point;

        Debug.DrawLine(transform.position, hit0.point, Color.red);
        Debug.DrawLine(hit0.point, hit1.point, Color.blue);
    }
}

This is the screenshot:


Red is the first line but blue line that should be the reflected one is absent.

yes, if the line is generated without distance from the camera, maybe the line isn’t show for the clipping plane Near of the camera

here there is not any Line Renderer

Because line renderer is not causing problem. Problem is with Ray casting.

Maybe Hit1 is null beacause it dont hit anything

I also tried if statement but nothing happened.

But what do you want to do esactly?

I want to make an aim line like in puzzle bobble

Ahhhh, ok i understand the error: you display the blue line on the top of the red one, in reverse:

Change this with

RaycastHit2D hit1 = Physics2D.Raycast(hit0.point, reflection + Vector2.right * 90);

I will try it. Hope it works. :smiling_face:

Anyway thanks a lot.

let me know :smile:

Alas, it didn’t work.

I am thinking that my whole concept is wrong. But can’t find another solution.

Vector2 reflection = Vector2.Reflect(hit0.point, hit0.normal);

Try this:
Vector2 reflection = Vector2.Reflect(hit0.point, hit0.normal * 1000);

I don’t think this can be a problem, but maybe change something

I’ll try.

It didn’t work.