Hi,
I want to attach a trail renderer(or two) to a spaceship for contrail funtimes. However I will be keeping my ship static on the Z axis(there will be some lateral movement) so no trail appears. Is there a way to get the trail visible on a non moving object?

Hmm, I can’t think of a way to use the trail renderer on a non-moving object.

You could, however, use a [line renderer][1] instead, and manually set the positions of each segment of the line yourself through a script.

In the scripting reference, the line renderer has the [SetPosition][2] function which would be perfect for your needs.

Edit 1: Below is the code that could simulate a trail renderer when an object is static. To test this out, make a new object, put a line renderer on it, and slap on this script (and add in the values of course). The values I used for testing were: 10 for trail resolution, (2,0,0) for offset and 0.15f for lag time.

Edit 2: Added the ‘RequiredComponent’ attribute so you don’t even have to add a Line Renderer manually. Also this new code viewer on Unity’s website is cutting off my code … gah. Where did the scroll-bar go?

Edit 3: Added functionality to the script so that you can choose which local direction the trail follows. Rotating the object now makes the trail orient itself correctly.

using UnityEngine;
using System.Collections;

public class ManualTrail : MonoBehaviour 
{
	public int trailResolution;
	LineRenderer lineRenderer;

	Vector3[] lineSegmentPositions;
	Vector3[] lineSegmentVelocities;

	// This would be the distance between the individual points of the line renderer
	public float offset;
	Vector3 facingDirection;

	public enum LocalDirections {XAxis, YAxis, ZAxis}
	public LocalDirections localDirectionToUse;

	// How far the points 'lag' behind each other in terms of position
	public float lagTime;

	Vector3 GetDirection()
	{
		switch(localDirectionToUse)
		{
			case LocalDirections.XAxis:
				return transform.right;
			case LocalDirections.YAxis:
				return transform.up;
			case LocalDirections.ZAxis:
				return transform.forward;
		}

		Debug.LogError("The variable 'localDirectionToUse' on the 'ManualTrail' script, located on object " + name + ", was somehow invalid. Please investigate!");
		return Vector3.zero;
	}

	// Use this for initialization
	void Start () 
	{
		lineRenderer = GetComponent<LineRenderer>();

		lineRenderer.SetVertexCount(trailResolution);

		lineSegmentPositions = new Vector3[trailResolution];
		lineSegmentVelocities = new Vector3[trailResolution];

		facingDirection = GetDirection();

		// Initialize our positions
		for (int i = 0; i < lineSegmentPositions.Length; i++)
		{
			lineSegmentPositions *= new Vector3();*

_ lineSegmentVelocities = new Vector3();_

* if (i == 0)*
* {*
* // Set the first position to be at the base of the transform*
_ lineSegmentPositions = transform.position;
* }
else*
* {
// All subsequent positions would be an offset of the original position.
lineSegmentPositions = transform.position + (facingDirection * (offset * i));
}
}
}*_

* // Update is called once per frame*
* void Update ()*
* {*
* facingDirection = GetDirection();*

* for (int i = 0; i < lineSegmentPositions.Length; i++)*
* {*
* if (i == 0)*
* {*
* // We always want the first position to be exactly at the original position*
_ lineSegmentPositions = transform.position;
* }
else*

* {
// All others will follow the original with the offset that you set up*

lineSegmentPositions = Vector3.SmoothDamp(lineSegmentPositions, lineSegmentPositions[i - 1] + (facingDirection * offset), ref lineSegmentVelocities*, lagTime);
}*_

* // Once we’re done calculating where our position should be, set the line segment to be in its proper place*
_ lineRenderer.SetPosition(i, lineSegmentPositions*);
}
}
}
[1]: http://docs.unity3d.com/Manual/class-LineRenderer.html*_

_*[2]: http://docs.unity3d.com/ScriptReference/LineRenderer.SetPosition.html*_

Hello and thank you Dan J-31 !

Your script is awesome. It is just what I needed. Just as I wrote on the comment above I got some issues when spawning an object with your manual trail script :

  • IsFinite(outDistanceForSort)
  • UnityEditor.DockArea:OnGUI()

I found the solution. It is so simple that I feel dumb. I just changed the size parameter of the line renderer. The size of the line renderer should be equal to the resolution parameter of your trail script.