- Home /
Line render is choppy
Hi,
So what I'm trying to do is be able to click and drag the mouse to draw a line. I'm using the Line Renderer to create the line. It seems to be working, but as I drag the mouse completely vertical it looks like the rendering is incomplete and choppy. Going horizontally is fine. Below is my code and a picture of what it looks like.
private var mouseLeftDownPoint : Vector2;
private var mouseLeftUpPoint : Vector2;
private var mouseDrag : boolean = false;
private var raycastLength : float = 500;
private var currentSelected : GameObject;
private var movementPath : LineRenderer;
private var numOfVertices : int = 1;
private var vertexNo : int = 0;
private var prevPnt : Vector3;
private var lineTimer : float = 0;
private var linePoints;
function Start()
{
movementPath = GetComponent(LineRenderer);
prevPnt = Vector3(-1000, -1000, 0);
linePoints = new Array(10000);
}
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
MouseLeftDown(Input.mousePosition);
}
if (Input.GetButtonUp("Fire1"))
{
MouseLeftUp(Input.mousePosition);
}
if (Input.GetButton("Fire1"))
{
// Check to see if mouse is being dragged
MouseLeftDrag(Input.mousePosition);
}
if(!mouseDrag)
{
lineTimer += Time.deltaTime;
if(lineTimer > 1)
{
lineTimer = 0;
}
}
}
function MouseLeftDown(screenPosition : Vector2)
{
mouseLeftDownPoint = screenPosition;
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(mouseLeftUpPoint);
if(Physics.Raycast(ray, hit, raycastLength))
{
Debug.Log(hit.point);
numOfVertices = 1;
vertexNo = 0;
movementPath.SetVertexCount(numOfVertices);
movementPath.SetPosition(0, hit.point);
}
}
function MouseLeftUp(screenPosition : Vector2)
{
mouseLeftUpPoint = screenPosition;
mouseDrag = false;
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(mouseLeftUpPoint);
if(Physics.Raycast(ray, hit, raycastLength))
{
Debug.Log(hit.point);
// We hit a unit, now select it
if(hit.collider.gameObject.name == "Spaceship")
{
currentSelected = hit.collider.gameObject;
currentSelected.SendMessage("Selected");
}
}
}
function MouseLeftDrag(screenPosition : Vector2)
{
if(screenPosition != mouseLeftDownPoint)
{
mouseDrag = true;
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(screenPosition);
if(Physics.Raycast(ray, hit, raycastLength))
{
Debug.Log("hit point is " + hit.point);
if(hit.point == prevPnt)
{
// Don't do anything if the mouse is staying put
}
else
{
movementPath.SetVertexCount(numOfVertices);
movementPath.SetPosition(vertexNo, hit.point);
numOfVertices++;
vertexNo++;
prevPnt = hit.point;
}
}
}
}
@script RequireComponent(LineRenderer)
I'm not sure if it's the way I'm drawing it, or is it because the way Line Renderer works that it doesn't work well when rendering lines in real-time?
Any input is welcome :D.
Answer by Julien-Lynge · Oct 18, 2011 at 06:23 PM
Reminator, You are drawing the line renderer when your mouse projection hits a plane in scene. That means that the line you are creating occupies the same space as the plane, and what you're seeing is an effect known as Z-fighting. This video should clear things up for you:
Haha, O$$anonymous$$, I can see how that problem occurs now. It's fixed now. Thanks!