- Home /
Question by
artificialfool · Jan 24, 2012 at 10:11 PM ·
crashlerplinerendererinterpolationsmoothing
Linear interpolation crash
I am running the following script which when properly attached to any objects of your choice and running the camera in orthographic mode basically draws smoothed lines on the path where the mouse was whiel mouse1 is held down. It works fine normally but any time I draw a loop with the mouse it crashes unity! any suggestions?
public var stroke : Transform;
private var toPaint : Array = new Array();
private var u : float;
public var smoothness : float = 0.1;
public var lineholder : GameObject;
private var toDraw : Array = new Array();
private var mousez : int;
private var liner : LineRenderer;
function Start()
{
}
function Update ()
{
if(Input.GetMouseButton(0))
{
mousePos = Input.mousePosition;
mousePos.z = 10;
worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = worldPos;
transform.position.z = 0;
var pos:Vector3 = transform.position;
toPaint.push(pos);
//Debug.Log("mousedown" + toPaint.length);
//var currstroke:Transform = Instantiate(stroke, pos, Quaternion.identity);
//currstroke.rigidbody.position = this.rigidbody.position;
//Debug.Log(toPaint.length);
}
else if(toPaint.length != 0)
{
var tempHolder : GameObject = Instantiate(lineholder, Vector3.zero, Quaternion.identity);
liner = tempHolder.AddComponent(LineRenderer);
print("still working");
for(var x: float = 0.0; x < 1; x += smoothness)
{
//toDraw.push((Instantiate(stroke, interpol(toPaint, x), Quaternion.identity)).transform.position);
toDraw.push(interpol(toPaint, x));
}
liner.SetVertexCount(toDraw.length);
print("still working2");
for(var y : int = 0; y < toDraw.length; y++)
{
liner.SetPosition(y, toDraw[y]);
}
toDraw.clear();
toPaint.clear();
}
}
static function interpol(arr : Array, u : float) : Vector3
{
if(arr.length == 1)
{ var v : Vector3 = arr[0]; return(v);}
var arr1 : Array = new Array();
var arr2 : Array = new Array();
if(arr.length > 1)
{
for(var x :int = 1; x < arr.length; x++)
{
arr1.push(arr[x]);
}
for(var y :int = 0; y < arr.length - 1; y++)
{
arr2.push(arr[y]);
}
}
return Vector3.Lerp(interpol(arr1, u), interpol(arr2, u), u);
}
Comment