Twisted Linerenderer

This script creates a square out of lines, but the lines are twisted somehow, like only one side of the line is rotating toward the camera.

var gridWidth = 5.0;
var gridExtents = 5.0;

private var grid : LineRenderer;

function Start() {
	grid = GetComponent( LineRenderer );
	if( grid ) CreateGrid();
}

function CreateGrid() {
	var upLeft = new Vector3( transform.position.x - gridExtents, transform.position.y + gridExtents, transform.position.z );
	var upRight = new Vector3( transform.position.x + gridExtents, transform.position.y + gridExtents, transform.position.z );
	var lowLeft = new Vector3( transform.position.x - gridExtents, transform.position.y - gridExtents, transform.position.z );
	var lowRight = new Vector3( transform.position.x + gridExtents, transform.position.y - gridExtents, transform.position.z );
	
	grid.SetVertexCount(5);
	
	grid.SetPosition(0, upLeft);
	grid.SetPosition(1, upRight);
	grid.SetPosition(2, lowRight);
	grid.SetPosition(3, lowLeft );
	grid.SetPosition(4, upLeft );
}

What could be causing this?

I think a screenshot would be a big help - I’m not really sure what you’re describing.

Even better—here’s an example project.

Sorry if I was ambiguous!

39360–1460–$lineproject_531.zip (171 KB)

No ideas? Looking closer I’ve noticed a couple things:

• The last line segment of the line renderer displays normally.
• The lines are twisted even without using the script (new project shows this below).
• The twisted lines seem to have one triangle that rotates TOWARD the camera, as would be expected, but the other triangle rotates AWAY from the camera. Normals?

EDIT: A line segment “twists” more or less depending on the relative angle of the segment after it. Is this a ‘feature’?

39418–1464–$lineproject_140.zip (166 KB)

The LineRenderer doesn’t try to mitre corners at all. The pair of vertices generated for a particular corner are placed perpendicular to the next segment of line, with no regard for the angle of the previous segment, so sharp turns can look pretty ugly.

There are a couple of options. You can either insert more vertices to turn the corner more smoothly, or you can use the Mesh API to write your own version of the LineRenderer with features to mitre corners properly.

Hmm, ok. Thanks for responding!