Reputation: 57
I have a small dilemma and I can't figure out what the problem is. I have a grid of cells ... I drag the mouse through them and create a connection between them with line renderer. The problem is that Line Renderer does not look ok, I will attach an image below to see what the problem is. Thank you very much.
public Grid grid;
public float connectionDistanceLimit = 3.185f; //0.91*3.5
public float snapDistance = 0.2f;
public LineRenderer connectionRenderer;
private List<Vector3> connectionPositions;
public List<GridCell> cellsOnScreen = new List<GridCell>();
public List<GridCell> connectedCells;
private GridCell lastConnectedCell;
private GridCell currentSelectedCell;
private bool conectionStarted = false;
IEnumerator Start()
{
yield return new WaitForEndOfFrame();
cellsOnScreen = grid.GetGridElements();
foreach (GridCell cell in cellsOnScreen)
cell.CellPointEvent += Cell_CellPointEvent;
}
private void Cell_CellPointEvent(bool enter, GridCell cell)
{
currentSelectedCell = cell;
if (conectionStarted)
return;
connectionPositions = new List<Vector3>();
connectedCells = new List<GridCell>();
connectedCells.Add(cell);
Vector3 positionToAdd = cell.transform.position;
positionToAdd.z = 0f;
connectionPositions.Add(positionToAdd);
cell.SetColor();
conectionStarted = true;
}
void Update()
{
if (Input.GetMouseButton(0))
{
if (connectionPositions != null)
{
if (lastConnectedCell != null)
{
if (lastConnectedCell != currentSelectedCell)
return;
}
List<Vector3> tempPositions = new List<Vector3>(connectionPositions);
Vector3 mPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 lastConnection = tempPositions[tempPositions.Count - 1];
mPosition.z = 0f;
lastConnection.z = 0f;
if (Vector3.Distance(lastConnection, mPosition) > connectionDistanceLimit)
{
Ray r = new Ray(lastConnection, (mPosition - lastConnection));
mPosition = r.GetPoint(connectionDistanceLimit);
}
if (Vector3.Distance(lastConnection, mPosition) < connectionDistanceLimit)
foreach (GridCell cell in cellsOnScreen)
{
if (!connectedCells.Contains(cell))
{
GridCell lastCell = connectedCells[connectedCells.Count - 1];
if ((cell.transform.position.y > lastCell.transform.position.y && cell.transform.position.x != lastCell.transform.position.x) ||
cell.transform.position.y < lastCell.transform.position.y && cell.transform.position.x != lastCell.transform.position.x)
{
}
else
{
if (Vector3.Distance(mPosition, cell.transform.position) <= snapDistance)
{
connectedCells.Add(cell);
connectionPositions.Add(cell.transform.position);
cell.isConnected = true;
cell.SetColor();
break;
}
}
}
}
lastConnection.z = 0f;
mPosition.z = 0f;
tempPositions.Add(mPosition);
connectionRenderer.positionCount = tempPositions.Count;
connectionRenderer.SetPositions(tempPositions.ToArray());
}
}
if (Input.GetMouseButtonUp(0))
{
lastConnectedCell = connectedCells[connectedCells.Count - 1];
}
}
In the inspector I look ok, but in Play Mode it makes this effect that I don't realize why. I thought position Z was a problem, but no, it's 0. Sorry for bad English.
Upvotes: 0
Views: 2281
Reputation: 11
I'm 7 months late, but it looks like your width is too small. I was just trying to debug this myself and the unity line renderer really doesn't play nice with low widths and closely adjacent positions. you either need to increase the width or find another solution to draw the line you want.
i recommend just computing the vertex positions yourself and rendering a model that way tho you can probably find some stuff on the unity store
Upvotes: 1