- Home /
Timetrail script creating 2 trails and not destroying the second
Hello, I am using the Timetrail script found on the wiki. It works well, however there is a problem. For each object that I instantiate with the script attached, it actually creates two "Trail" objects, and only one of them gets destroyed when my object gets destroyed. The others stays in the game hierarchy until the game is turned off.
I was wondering if anyone has come across this and knows how to fix it? I don't quite understand the code in the script, so any help is appreciated.
Below is the code for the script:
var points = Array();
var emit = true;
var emitTime = 0.00;
var material : Material;
var lifeTime = 1.00;
var colors : Color[];
var sizes : float[];
var uvLengthScale = 0.01;
var higherQualityUVs = true;
var movePixelsForRebuild = 6;
var maxRebuildTime = 0.1;
var minVertexDistance = 0.10;
var maxVertexDistance = 10.00;
var maxAngle = 3.00;
var autoDestruct = false;
private var o : GameObject;
private var lastPosition : Vector3;
private var lastCameraPosition1 : Vector3;
private var lastCameraPosition2 : Vector3;
private var lastRebuildTime = 0.00;
private var lastFrameEmit = true;
class Point
{
var timeCreated = 0.00;
var position : Vector3;
var lineBreak = false;
}
function Start ()
{
lastPosition = transform.position;
o = new GameObject("Trail");
o.transform.parent = null;
o.transform.position = Vector3.zero;
o.transform.rotation = Quaternion.identity;
o.transform.localScale = Vector3.one;
o.AddComponent(MeshFilter);
o.AddComponent(MeshRenderer);
o.renderer.material = material;
}
function OnEnable ()
{
lastPosition = transform.position;
o = new GameObject("Trail");
o.transform.parent = null;
o.transform.position = Vector3.zero;
o.transform.rotation = Quaternion.identity;
o.transform.localScale = Vector3.one;
o.AddComponent(MeshFilter);
o.AddComponent(MeshRenderer);
o.renderer.material = material;
}
function OnDisable ()
{
Destroy(o);
}
function Update ()
{
if(emit && emitTime != 0)
{
emitTime -= Time.deltaTime;
if(emitTime == 0) emitTime = -1;
if(emitTime < 0) emit = false;
}
if(!emit && points.length == 0 && autoDestruct)
{
Destroy(o);
Destroy(gameObject);
}
// early out if there is no camera
if(!Camera.main) return;
re = false;
// if we have moved enough, create a new vertex and make sure we rebuild the mesh
theDistance = (lastPosition - transform.position).magnitude;
if(emit)
{
if(theDistance > minVertexDistance)
{
make = false;
if(points.length < 3)
{
make = true;
}
else
{
l1 = points[points.length - 2].position - points[points.length - 3].position;
l2 = points[points.length - 1].position - points[points.length - 2].position;
if(Vector3.Angle(l1, l2) > maxAngle || theDistance > maxVertexDistance) make = true;
}
if(make)
{
var z = new Point();
z.position = transform.position;
z.timeCreated = Time.time;
points.Add(z);
lastPosition = transform.position;
}
else
{
points[points.length - 1].position = transform.position;
points[points.length - 1].timeCreated = Time.time;
}
}
else if(points.length > 0)
{
points[points.length - 1].position = transform.position;
points[points.length - 1].timeCreated = Time.time;
}
}
if(!emit && lastFrameEmit && points.length > 0) points[points.length - 1].lineBreak = true;
lastFrameEmit = emit;
// approximate if we should rebuild the mesh or not
if(points.length > 1)
{
cur1 = Camera.main.WorldToScreenPoint(points[0].position);
lastCameraPosition1.z = 0;
cur2 = Camera.main.WorldToScreenPoint(points[points.length - 1].position);
lastCameraPosition2.z = 0;
distance = (lastCameraPosition1 - cur1).magnitude;
distance += (lastCameraPosition2 - cur2).magnitude;
if(distance > movePixelsForRebuild || Time.time - lastRebuildTime > maxRebuildTime)
{
re = true;
lastCameraPosition1 = cur1;
lastCameraPosition2 = cur2;
}
}
else
{
re = true;
}
if(re)
{
lastRebuildTime = Time.time;
i = 0;
for(var p : Point in points)
{
// cull old points first
if(Time.time - p.timeCreated > lifeTime)
{
points.RemoveAt(i);
}
i++;
}
if(points.length > 1)
{
var newVertices : Vector3[] = new Vector3[points.length * 2];
var newUV : Vector2[] = new Vector2[points.length * 2];
var newTriangles : int[] = new int[(points.length - 1) * 6];
var newColors : Color[] = new Color[points.length * 2];
i = 0;
var curDistance = 0.00;
for(var p : Point in points)
{
time = (Time.time - p.timeCreated) / lifeTime;
if(colors.length > 0)
{
colorTime = time * (colors.length - 1);
min = Mathf.Floor(colorTime);
max = Mathf.Clamp(Mathf.Ceil(colorTime), 1, colors.length - 1);
lerp = Mathf.InverseLerp(min, max, colorTime);
color = Color.Lerp(colors[min], colors[max], lerp);
}
else
{
color = Color.Lerp(Color.white, Color.clear, time);
}
if(sizes.length > 0)
{
sizeTime = time * (sizes.length - 1);
min = Mathf.Floor(sizeTime);
max = Mathf.Clamp(Mathf.Ceil(sizeTime), 1, sizes.length - 1);
lerp = Mathf.InverseLerp(min, max, sizeTime);
size = Mathf.Lerp(sizes[min], sizes[max], lerp);
}
else
{
size = 1;
}
if(i == 0) lineDirection = p.position - points[i + 1].position;
else lineDirection = points[i - 1].position - p.position;
vectorToCamera = Camera.main.transform.position - p.position;
perpendicular = Vector3.Cross(lineDirection, vectorToCamera).normalized;
newVertices[i * 2] = p.position + (perpendicular * (size * 0.5));
newVertices[(i * 2) + 1] = p.position + (-perpendicular * (size * 0.5));
newColors[i * 2] = newColors[(i * 2) + 1] = color;
newUV[i * 2] = Vector2(curDistance * uvLengthScale, 0);
newUV[(i * 2) + 1] = Vector2(curDistance * uvLengthScale, 1);
if(i > 0 && !points[i - 1].lineBreak)
{
if(higherQualityUVs) curDistance += (p.position - points[i - 1].position).magnitude;
else curDistance += (p.position - points[i - 1].position).sqrMagnitude;
newTriangles[(i - 1) * 6] = (i * 2) - 2;
newTriangles[((i - 1) * 6) + 1] = (i * 2) - 1;
newTriangles[((i - 1) * 6) + 2] = i * 2;
newTriangles[((i - 1) * 6) + 3] = (i * 2) + 1;
newTriangles[((i - 1) * 6) + 4] = i * 2;
newTriangles[((i - 1) * 6) + 5] = (i * 2) - 1;
}
i ++;
}
var mesh : Mesh = o.GetComponent(MeshFilter).mesh;
mesh.Clear();
mesh.vertices = newVertices;
mesh.colors = newColors;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}
}
}
Your answer
Follow this Question
Related Questions
Blocking particles then destroy then reduce health 0 Answers
bind instantiated particles to an object 1 Answer
Instantiate spawns 2 instead of 1, help? 2 Answers
Instantiated Particles reverse direction. Why? 1 Answer
Streaming Game Content 1 Answer