Help with infinite line
Hi, i am having trouble making a 2d game with 4 separate lines on which the player(dot, or cube) would move, they need to be infinite lines. i have some ideas about creating them with line renderer? not looking for a specific solution, rather a direction or advice on where to start.
if you can use linerenderer it is a easy way of achieving this, you will have to create a new linerenderer whenever you leave a blank space between lines, if you have any questions ask them :)
can you help with this, how would you create 4 different line renderers that go to infinity, cross each other but never overlap and go one over each other
yes i can help you but i need to know if the lines get cut like in your example since that ahs extra work
Answer by tormentoarmagedoom · May 22, 2019 at 11:44 AM
Hello.
I dnt know if its the best solution, but if i have to do this i would do little different.
For the image, i can imagine all time the lines go in same direction (they crosss each other, but always go ahead), so i would have several lines segments as a prefab, and instantiate them continusly in front and delete then once out of scene.
This way, will have only few line objects in scene at same time, and can be instantiating new lines to the infinite.
Bye!
i thought of this, but then lines would have to end at the same point, meaning they would have to be evenly spaced, maybe illustration is not the best, but lines can go left and right and end at different positions, so the space between them wont always be same
Answer by xxmariofer · May 28, 2019 at 08:35 PM
here is the code, try this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private LineRenderer[] lines;
private int currentPosition;
void Awake()
{
currentPosition = 2;
lines = new LineRenderer[4];
for (int i = 0; i < 4; i++)
{
GameObject lineParent = new GameObject();
lines[i] = lineParent.AddComponent<LineRenderer>();
lines[i].startWidth = 0.5f;
lines[i].endWidth = 0.5f;
lines[i].SetPosition(0, new Vector3(i, 0, 0));
lines[i].SetPosition(1, new Vector3(i, 0, 1));
}
StartCoroutine(NextIteration());
}
public IEnumerator NextIteration()
{
while(true)
{
yield return new WaitForSeconds(1f);
if(Random.Range(0, 5) == 0)
{
Shuffle();
}
for (int i = 0; i < 4; i++)
{
lines[i].positionCount = currentPosition + 1;
lines[i].SetPosition(currentPosition, new Vector3(i, 0, currentPosition));
}
currentPosition++;
}
}
private void Shuffle()
{
int index1 = Random.Range(0, 4);
int index2;
do { index2 = Random.Range(0, 4); } while (index2 == index1);
LineRenderer tmp = lines[index1];
lines[index1] = lines[index2];
lines[index2] = tmp;
}
}
no sorry, not even close. i want lines to be going independently one from another, to go left and right and to cut other lines totally random. i am not looking for a solution written in code, rather an idea how to get to that point. the main problem is how to detect if a new generated position of a line, will be at other line.
You need to br more specific since the code i provided is totally random, they cut each other and they go left and right, unless you tell us what the exact diference of my solution and what you want to achieve we cant help you, with the codr i provided yoy can difectly have that same drawn as yours
Answer by Ditech · May 30, 2019 at 07:33 PM
so far i have managed to create 4 lines, randomly generated to always stay within the borders of the screen. but can't quite figure how to stop them going one over another... here is screenshot maybe it will help explain better what is happening.
Your answer
Follow this Question
Related Questions
How to create an Animated 2D Line to help the user aim and shoot? 1 Answer
Unity Linerenderer jaggy lines 0 Answers
How to remove lines occurring between images in 6-sided skyboxes 0 Answers
Infinite Scrollview and Read A File Line By Line 0 Answers
How to remove the "lines" that appear sometimes when previewing the game? 1 Answer