I have a Line Renderer component with 4 points, I would like to disable and enable the last 2 points at a certain time. My question is, how would I in code (C#) enable and disable individual Line Renderer points?
Thanks in advance!
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityI have a Line Renderer component with 4 points, I would like to disable and enable the last 2 points at a certain time. My question is, how would I in code (C#) enable and disable individual Line Renderer points?
Thanks in advance!
As mentioned in a comment above, you can't enable or disable individual points in the LineRenderer. But, you can change/add/remove points dynamically, which can get a similar effect.
Here's a sample script that builds a random zig-zag path by adding segments to the end of the line, then progressively chops the line back until it disappears completely.
[RequireComponent(typeof(LineRenderer))]
public class RandomZig : MonoBehaviour {
LineRenderer _line;
public int maxSize = 30;
public float delay = 0.25f;
// Use this for initialization
void Start () {
_line = GetComponent<LineRenderer>();
StartCoroutine(AddSegments());
}
IEnumerator AddSegments()
{
float previous = 0f;
for(int i = 0; i < maxSize; i++)
{
_line.SetVertexCount(i + 1);
float wander = previous + (Random.value - 0.5f) * 2f;
_line.SetPosition(i, Vector3.forward * i + Vector3.right * wander);
// Note you can use SetPositions if you need to assign a whole array at once.
previous = wander;
yield return new WaitForSeconds(delay);
}
StartCoroutine(RemoveSegments());
}
IEnumerator RemoveSegments()
{
for(int i = maxSize - 1; i > 0; i--)
{
_line.SetVertexCount(i);
yield return new WaitForSeconds(delay);
}
}
}
.SetVertexCount(NUMBER)
, it is .numPositions = NUMBER
\$\endgroup\$
Feb 12, 2017 at 18:57