Hi, m very new to unity3D,, my problem is how to Draw a line on mouse movement using dynamic array in c# or javascript..please help me...
Two options that come to mind are Vectrocity or Unity built in LineRenderer. Both can be scripted (For line renderer - look here).
In this question SirVictory said about how he used Vectrosity: "For those who have vectrosity, look at the Draw Lines Touch script as a ref, that's how my line is drawn". So try and look it up. It sounds like it helped him a lot.
Since Cyb3rMakiak mentioned it, here's the DrawLinesTouch script with a few changes so it works with mouse input instead. Requires Vectrosity to actually work, of course...it could be adapted to work with the LineRenderer, but it would need some reworking since the LineRenderer only does the Vectrosity equivalent of continuous lines, rather than the discrete lines used here. (Essentially discrete line segments are used so the unused parts of the array aren't drawn. It's possible to do that with a continuous line, but it requires some fiddling with line segment colors involving transparency. Either that or you have to continuously resize the array and the line, which is not so good for performance.)
var lineMaterial : Material;
var maxPoints = 300;
var lineWidth = 2.0;
var minPixelMove = 5; // Must move at least this many pixels per sample for a new segment to be recorded
private var linePoints : Vector2[];
private var line : VectorLine;
private var lineIndex = 0;
private var previousPosition : Vector2;
private var sqrMinPixelMove : int;
private var canDraw = false;
function Start () {
if (maxPoints%2 != 0) maxPoints++; // No odd numbers
linePoints = new Vector2[maxPoints];
line = new VectorLine("DrawnLine", linePoints, lineMaterial, lineWidth);
sqrMinPixelMove = minPixelMove*minPixelMove;
}
function Update () {
var mousePos = Input.mousePosition;
if (Input.GetMouseButtonDown(0)) {
Vector.ZeroPointsInLine(line);
previousPosition = mousePos;
lineIndex = 0;
Vector.DrawLine(line);
canDraw = true;
}
else if (Input.GetMouseButton(0) && (mousePos - previousPosition).sqrMagnitude > sqrMinPixelMove && canDraw) {
linePoints[lineIndex++] = previousPosition;
linePoints[lineIndex++] = mousePos;
previousPosition = mousePos;
if (lineIndex >= maxPoints) canDraw = false;
Vector.DrawLine(line);
}
}