SHARE
TWEET

Untitled

a guest Dec 8th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private void Awake()
  2.     {
  3.         //create an object and mesh for the trail
  4.         trail = new GameObject("Trail", new[] { typeof(MeshRenderer), typeof(MeshFilter), typeof(PolygonCollider2D) });
  5.        
  6.         mesh = trail.GetComponent<MeshFilter>().mesh = new Mesh();
  7.         trail.GetComponent<Renderer>().material = trailMaterial;
  8.  
  9.         //get and set the polygon collider on this trail.
  10.         collider = trail.GetComponent<PolygonCollider2D>();
  11.         collider.isTrigger = colliderIsTrigger;
  12.         //trail.transform.parent = transform;
  13.  
  14.         //get the transform of the object this script is attatched to
  15.         trans = transform;
  16.  
  17.         //set the first center position as the current position
  18.         centerPositions = new LinkedList<Vector3>();
  19.         centerPositions.AddFirst(trans.position);
  20.  
  21.         leftVertices = new LinkedList<Vertex>();
  22.         rightVertices = new LinkedList<Vertex>();
  23.     }
  24.  
  25.     private void Update()
  26.     {
  27.         if (!pausing)
  28.         {
  29.             //set the mesh and adjust widths if vertices were added or removed
  30.             if (TryAddVertices() | TryRemoveVertices())
  31.             {
  32.  
  33.                 if (widthStart != widthEnd)
  34.                 {
  35.                     SetVertexWidths();
  36.                 }
  37.  
  38.                 SetMesh();
  39.             }
  40.         }
  41.     }
  42.  
  43.     //************
  44.     //
  45.     // Private Methods
  46.     //
  47.     //************
  48.  
  49.     /// <summary>
  50.     /// Adds new vertices if the object has moved more than 'vertexDistanceMin' from the most recent center position.
  51.     /// If a pair of vertices has been added, this method returns true.
  52.     /// </summary>
  53.     private bool TryAddVertices()
  54.     {
  55.         bool vertsAdded = false;
  56.  
  57.         //check if the current position is far enough away (> 'vertexDistanceMin') from the most recent position where two vertices were added
  58.         if ((centerPositions.First.Value - trans.position).sqrMagnitude > vertexDistanceMin * vertexDistanceMin)
  59.         {
  60.             Debug.Log("tray " + trans.position);
  61.             //calculate the normalized direction from the 1) most recent position of vertex creation to the 2) current position
  62.             Vector3 dirToCurrentPos = (trans.position - centerPositions.First.Value).normalized;
  63.  
  64.             //calculate the positions of the left and right vertices --> they are perpendicular to 'dirToCurrentPos' and 'renderDirection'
  65.             Vector3 cross = Vector3.Cross(renderDirection, dirToCurrentPos);
  66.             Vector3 leftPos = trans.position + (cross * -widthStart * 0.5f);
  67.             Vector3 rightPos = trans.position + (cross * widthStart * 0.5f);
  68.  
  69.             //create two new vertices at the calculated positions
  70.             leftVertices.AddFirst(new Vertex(leftPos, trans.position, (leftPos - trans.position).normalized));
  71.             rightVertices.AddFirst(new Vertex(rightPos, trans.position, (rightPos - trans.position).normalized));
  72.  
  73.             //add the current position as the most recent center position
  74.             centerPositions.AddFirst(trans.position);
  75.             vertsAdded = true;
  76.         }
  77.  
  78.         return vertsAdded;
  79.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top