C
C#3y ago
c_ccc

Line Renderer Help

I am trying to get this code to work There is no bullet in the map at the start of the game when the player shoots, a bullet prefab is spawned i want a line to render from pos1 ( a set point ) to the spawned bullet and repeat everytime
public class LinePositions : MonoBehaviour
{
public LineRenderer theline;
public Transform pos11;
public GameObject BULLET;

// Start is called before the first frame update
void Start()
{
BULLET = GameObject.FindGameObjectWithTag("Bullet");
theline.enabled = false;
theline.positionCount = 2;
}

// Update is called once per frame
void Update()
{
if(BULLET != null)
{
theline.SetPosition(0, pos11.position);
theline.SetPosition(1, BULLET.transform.position);
theline.enabled = true;
}

}
}
public class LinePositions : MonoBehaviour
{
public LineRenderer theline;
public Transform pos11;
public GameObject BULLET;

// Start is called before the first frame update
void Start()
{
BULLET = GameObject.FindGameObjectWithTag("Bullet");
theline.enabled = false;
theline.positionCount = 2;
}

// Update is called once per frame
void Update()
{
if(BULLET != null)
{
theline.SetPosition(0, pos11.position);
theline.SetPosition(1, BULLET.transform.position);
theline.enabled = true;
}

}
}
3 Replies
Anchy
Anchy3y ago
So what issues are you having? Is there an error? What is expected vs what is happening? have you stepped through the code to ensure BULLET is not null? also is theline null? If there is no bullet at the start of the game and the LinePositions script is attached at the start of the game, a game object with the tag Bullet will never be found
i love cat(mull-rom splines)
when in doubt try the vfx graph not saying it'll work for this situation but look it up, see if it fits also all you really need to do is parent the linerenderer to the bullet prefab in editor no scripting needed
c_ccc
c_cccOP3y ago
no so the line is attached to the camera and exists when the player shoots, the bullet prefab spawns my aim is fo quite simply activate the line and attach it to the prefab when it spawns u legend<3
Want results from more Discord servers?
Add your server