Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by junlee · Mar 31, 2012 at 09:49 AM · instantiateparticlesdestroy

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;
       }
    }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges