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
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