- Home /
Question by
Walemax · Dec 18, 2020 at 02:07 PM ·
scripting problemscript.unity 2dmultipleline renderer
Creating multiple lines using multiple Line Renders, but all are receiving the same update.
My goal: to create multiple lines and placing them onto a grid, create random noise to change the length and direction of each line.
Basically I am trying to recreate "Drift" macOS screensaver, or called Anemona.
I think the problem is from these lines of code:
Lines.Add(new Line(
new Vector2(x,y),
new Vector2(x,y+1),
counterX+counterY
));
-------------------------------------------------------
for (int index = 0; index < Lines.Count; index++)
{
Vector2 location = Lines[index].getLocation();
//noiseP = Mathf.PerlinNoise(location.x + 0.1f * random, location.y + 0.1f * random);
Lines[index].updateEnd(new Vector2(location.x + random, location.y + random));
}
A picture of what is not supposed to happen:
A description of what should happen: Each line will have a random direction.
Line.cs class:
using UnityEngine;
public class Line
{
private Vector2 start;
private Vector2 end;
private Color startColor;
private Color endColor;
GameObject myLine;
LineRenderer lr;
public Line(Vector2 start, Vector2 end, float position)
{
myLine = new GameObject();
myLine.name = "Line " + position;
myLine.transform.position = start;
myLine.AddComponent<LineRenderer>();
lr = myLine.GetComponent<LineRenderer>();
lr.material = new Material(Shader.Find("Sprites/Default"));
lr.startWidth = 0.05f;
lr.endWidth = 0.05f;
lr.SetPosition(0, start);
this.start = start;
lr.SetPosition(1, end);
this.end = end;
}
public void updateEnd(Vector2 end)
{
this.end = end;
lr.startWidth = 0.05f;
lr.endWidth = 0.05f;
lr.SetPosition(1, end);
}
public void updateLength(float ratio)
{
float dx = end.x - start.x;
float dy = end.y - start.y;
float mag = (dx * dx) - (dy * dy);
Vector2 endR = new Vector2(
((end.x * ratio) * dx / mag),
((end.y * ratio) * dx / mag)
);
lr.SetPosition(1, endR);
}
public Vector2 getLocation()
{
return end;
}
}
Main line engine:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LineCreator : MonoBehaviour
{
List<Line> Lines = new List<Line>();
public int width = 20;
public int height = 20;
public int dpi;
// Start is called before the first frame update
void Start()
{
float counterX = 0;
float counterY = 0;
for (int x = 0; x < width; x++)
{
counterY = 0;
for (int y = 0; y < width; y++)
{
Lines.Add(new Line(
new Vector2(x,y),
new Vector2(x,y+1),
counterX+counterY
));
counterY += 0.1f;
}
counterX += 1f;
}
}
// Update is called once per frame
void Update()
{
float noiseP;
if (Input.GetMouseButtonDown(1))
{
float random = Random.Range(-1f, 1f);
for (int index = 0; index < Lines.Count; index++)
{
Vector2 location = Lines[index].getLocation();
//noiseP = Mathf.PerlinNoise(location.x + 0.1f * random, location.y + 0.1f * random);
Lines[index].updateEnd(new Vector2(location.x + random, location.y + random));
}
}
}
}
18-302.png
(4.1 kB)
Comment
Answer by Jaxcap · Dec 18, 2020 at 02:20 PM
I didn't look into the math part of it, but just taking a look at the Update function, I feel like you're using the same random value for every Line in that List. Maybe if you regenerate the random number every time you run the loop, it'll fix it?
float random;
for (int index = 0; index < Lines.Count; index++)
{
random = Random.Range(-1f, 1f);
Vector2 location = Lines[index].getLocation();
//noiseP = Mathf.PerlinNoise(location.x + 0.1f * random, location.y + 0.1f * random);
Lines[index].updateEnd(new Vector2(location.x + random, location.y + random));
}