Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Tron Trail Start & End Width?

Discussion in 'Scripting' started by cherub, Oct 10, 2014.

  1. cherub

    cherub

    Joined:
    Apr 26, 2006
    Posts:
    493
    I cant seem to be able to figure out how to add start & end width to this script. Been trying forever!

    Code (CSharp):
    1. /*
    2. Generates a trail that is always facing upwards using the scriptable mesh interface.
    3. vertex colors and uv's are generated similar to the builtin Trail Renderer.
    4. To use it
    5. 1. create an empty game object
    6. 2. attach this script and a MeshRenderer
    7. 3. Then assign a particle material to the mesh renderer
    8. */
    9. var height = 2.0;
    10. public var time = 2.0;
    11. var alwaysUp = false;
    12. var minDistance = 0.1;
    13.  
    14. var startColor = Color.white;
    15. var endColor = Color (1, 1, 1, 0);
    16.  
    17.  
    18. class TronTrailSection
    19. {
    20.     var point : Vector3;
    21.     var upDir : Vector3;
    22.     var time : float;
    23. }
    24.  
    25. private var sections = new Array();
    26.  
    27. var lastPosition : Vector3;
    28. var myDirection : Vector3;
    29.  
    30.  
    31. function LateUpdate () {
    32.     var position = transform.position;
    33.     position.y -= height/2;
    34.     var now = Time.time;
    35.  
    36.  
    37.     // Remove old sections
    38.     while (sections.length > 0 && now > sections[sections.length - 1].time + time)
    39.     {
    40.         sections.Pop();
    41.     }
    42.  
    43.     // Add a new trail section
    44.     if (sections.length == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance)
    45.     {
    46.         var section = TronTrailSection ();
    47.    
    48.         section.point = position;
    49.         if (alwaysUp)
    50.             section.upDir = Vector3.up;
    51.         else
    52.             section.upDir = transform.TransformDirection(Vector3.up);
    53.         section.time = now;
    54.         sections.Unshift(section);
    55.     }
    56.    
    57.     // Rebuild the mesh
    58.     var mesh : Mesh = GetComponent(MeshFilter).mesh;
    59.     mesh.Clear();
    60.    
    61.     // We need at least 2 sections to create the line
    62.     if (sections.length < 2)
    63.         return;
    64.  
    65.     var vertices = new Vector3[sections.length * 2];
    66.     var colors = new Color[sections.length * 2];
    67.     var uv = new Vector2[sections.length * 2];
    68.    
    69.     var previousSection : TronTrailSection = sections[0];
    70.     var currentSection : TronTrailSection = sections[0];
    71.  
    72.     // Use matrix instead of transform.TransformPoint for performance reasons
    73.     var localSpaceTransform = transform.worldToLocalMatrix;
    74.  
    75.     // Generate vertex, uv and colors
    76.     for (var i=0;i<sections.length;i++)
    77.     {
    78.            
    79.        
    80.         previousSection = currentSection;
    81.         currentSection = sections[i];
    82.        
    83.         var ratio = i * 1f / sections.length;
    84.  
    85.         // Calculate upwards direction
    86.         var upDir = currentSection.upDir;
    87.        
    88.         // Generate vertices
    89.         vertices[i * 2 + 0] = localSpaceTransform.MultiplyPoint(currentSection.point);
    90.         vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(currentSection.point + upDir * height);
    91.        
    92.         //var curDistance = 0.00;
    93.        
    94.         uv[i * 2 + 0] = new Vector2(ratio , 0);
    95.         uv[i * 2 + 1] = new Vector2(ratio, 1);
    96.  
    97.  
    98.         // fade colors out over time
    99.         //var interpolatedColor = Color.Lerp(startColor, endColor, u);
    100.         colors[i * 2 + 0] = startColor;
    101.         colors[i * 2 + 1] = endColor;
    102.     }
    103.  
    104.     // Generate triangles indices
    105.     var triangles = new int[(sections.length - 1) * 2 * 3];
    106.     for (i=0;i<triangles.length / 6;i++)
    107.     {
    108.         triangles[i * 6 + 0] = i * 2;
    109.         triangles[i * 6 + 1] = i * 2 + 1;
    110.         triangles[i * 6 + 2] = i * 2 + 2;
    111.  
    112.         triangles[i * 6 + 3] = i * 2 + 2;
    113.         triangles[i * 6 + 4] = i * 2 + 1;
    114.         triangles[i * 6 + 5] = i * 2 + 3;
    115.     }
    116.  
    117.     // Assign to mesh  
    118.     mesh.vertices = vertices;
    119.     mesh.colors = colors;
    120.     mesh.uv = uv;
    121.     mesh.triangles = triangles;
    122. }
    123.  
    124. @script RequireComponent (MeshFilter)
    125.  
    i think it has something to do with this bit?

    Code (csharp):
    1.  
    2.         // Generate vertices
    3.         vertices[i * 2 + 0] = localSpaceTransform.MultiplyPoint(currentSection.point);
    4.         vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(currentSection.point + upDir * height);
    5.  
     
  2. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    Have you tried TrailRenderer component? Doesn't require script and has tons of settings including setting end width (time it takes to disappear).
     
  3. cherub

    cherub

    Joined:
    Apr 26, 2006
    Posts:
    493
    that is what i want this , and the extruded mesh script, to do. :)
    the trail renderer does the "always face the camera" thing. these scripts create meshes that you can use.
     
  4. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    Are you trying to make it check for collision? E.g If object hits into trail he explodes? (Like in Tron movie it was actually happening)

    If that so, you can make your script spawn invisible cubes which are stripped down to box colliders, which then check for any objects. That might be expensive to do, but it might be worth a shot.
     
  5. cherub

    cherub

    Joined:
    Apr 26, 2006
    Posts:
    493
    no collision. just simple start and end width
     
  6. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    Soo....did you solve the problem with TrailRenderer?
     
  7. cherub

    cherub

    Joined:
    Apr 26, 2006
    Posts:
    493
    I also noticed that the start color and end color are actually top edge and bottom edge color.
    if that were working correctly i bet i could figure out the rest regarding the Tron Trail script.
     
  8. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    How about assigning a custom material with colors you need?
     
  9. cherub

    cherub

    Joined:
    Apr 26, 2006
    Posts:
    493
    cant get into how im using it. really just need help with the subject at hand