Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void Awake()
- {
- //create an object and mesh for the trail
- trail = new GameObject("Trail", new[] { typeof(MeshRenderer), typeof(MeshFilter), typeof(PolygonCollider2D) });
- mesh = trail.GetComponent<MeshFilter>().mesh = new Mesh();
- trail.GetComponent<Renderer>().material = trailMaterial;
- //get and set the polygon collider on this trail.
- collider = trail.GetComponent<PolygonCollider2D>();
- collider.isTrigger = colliderIsTrigger;
- //trail.transform.parent = transform;
- //get the transform of the object this script is attatched to
- trans = transform;
- //set the first center position as the current position
- centerPositions = new LinkedList<Vector3>();
- centerPositions.AddFirst(trans.position);
- leftVertices = new LinkedList<Vertex>();
- rightVertices = new LinkedList<Vertex>();
- }
- private void Update()
- {
- if (!pausing)
- {
- //set the mesh and adjust widths if vertices were added or removed
- if (TryAddVertices() | TryRemoveVertices())
- {
- if (widthStart != widthEnd)
- {
- SetVertexWidths();
- }
- SetMesh();
- }
- }
- }
- //************
- //
- // Private Methods
- //
- //************
- /// <summary>
- /// Adds new vertices if the object has moved more than 'vertexDistanceMin' from the most recent center position.
- /// If a pair of vertices has been added, this method returns true.
- /// </summary>
- private bool TryAddVertices()
- {
- bool vertsAdded = false;
- //check if the current position is far enough away (> 'vertexDistanceMin') from the most recent position where two vertices were added
- if ((centerPositions.First.Value - trans.position).sqrMagnitude > vertexDistanceMin * vertexDistanceMin)
- {
- Debug.Log("tray " + trans.position);
- //calculate the normalized direction from the 1) most recent position of vertex creation to the 2) current position
- Vector3 dirToCurrentPos = (trans.position - centerPositions.First.Value).normalized;
- //calculate the positions of the left and right vertices --> they are perpendicular to 'dirToCurrentPos' and 'renderDirection'
- Vector3 cross = Vector3.Cross(renderDirection, dirToCurrentPos);
- Vector3 leftPos = trans.position + (cross * -widthStart * 0.5f);
- Vector3 rightPos = trans.position + (cross * widthStart * 0.5f);
- //create two new vertices at the calculated positions
- leftVertices.AddFirst(new Vertex(leftPos, trans.position, (leftPos - trans.position).normalized));
- rightVertices.AddFirst(new Vertex(rightPos, trans.position, (rightPos - trans.position).normalized));
- //add the current position as the most recent center position
- centerPositions.AddFirst(trans.position);
- vertsAdded = true;
- }
- return vertsAdded;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement