how can I make it so that the lines I am rendering do not disappear when it spawns a new one? I am creating lines from one point to another in the correct position moving along the screen on the correct interval but the lines disappear one after another? I am trying to create a gamified graphical simulation of a stock chart. Thank you to anyone who helps.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class line_rend : MonoBehaviour
{
private LineRenderer lineRend;
float candle_y;
public float stop_running;
public float candle_speed;
public float candle_delay;
public float candle_rate;
public float candle_volat;
float candle_x;
// Start is called before the first frame update
void Awake()
{
candle_y = Random.Range(-4.5f, 4.5f);
lineRend = GetComponent<LineRenderer>();
candle_x = -11.0f;
InvokeRepeating("Spawn_Candle", candle_speed, candle_delay);
}
public void Spawn_Candle()
{
lineRend.startWidth = 0.1f;
lineRend.endWidth = 0.1f;
lineRend.SetPosition(0, new Vector3(candle_x, candle_y, 0f));
candle_y = Random.Range((0f - candle_volat), candle_volat);
candle_x = candle_rate;
lineRend.SetPosition(1, new Vector3(candle_x, candle_y, 0f));
}
}
CodePudding user response:
I think you are reusing the same line repeatedly.
Instead, save the line as a prefab and instantiate a new one when you need them.
A every brief example:
public Transform linePrefab; <--Configurate the prefab in Inspector
GameObject lineObj;
LineRenderer lineRend;
public void Spawn_Candle()
{
lineObj = Instantiate(linePrefab);
lineRend = lineObj.GetComponent<LineRenderer>();
lineRend.startWidth = 0.1f;
lineRend.endWidth = 0.1f;
lineRend.SetPosition(0, new Vector3(candle_x, candle_y, 0f));
candle_y = Random.Range((0f - candle_volat), candle_volat);
candle_x = candle_rate;
lineRend.SetPosition(1, new Vector3(candle_x, candle_y, 0f));
}
CodePudding user response:
As mentioned in this answer you are currently always overwriting your existing line with new coordinates.
However, a stock chart is a continuous line and you definitely do not want to spawn a new linerenderer for each stroke.
You rather simply want to add more points.
public class line_rend : MonoBehaviour
{
private LineRenderer lineRend;
// Personal choice but if I have coordinates I'd rather store them in a single field
Vector2 candle;
public float stop_running;
public float candle_speed;
public float candle_delay;
public float candle_rate;
public float candle_volat;
// Start is called before the first frame update
void Awake()
{
candle.y = Random.Range(-4.5f, 4.5f);
lineRend = GetComponent<LineRenderer>();
lineRend.startWidth = 0.1f;
lineRend.endWidth = 0.1f;
candle.x = -11.0f;
// Use nameof it prevents trouble when you rename the method later
InvokeRepeating(nameof(Spawn_Candle), candle_speed, candle_delay);
}
public void Spawn_Candle()
{
// Add a new position
lineRend.positionCount = 1;
candle.y = Random.Range(-candle_volat, candle_volat);
candle.x = candle_rate;
// only set the new position
// It already is connected to the previous one
lineRend.SetPosition(lineRend.positionCount - 1, candle);
}
}