Overview

Spline Trail Renderer is a simple to use component for Unity that let you have truly smooth trails combined with advanced parameters not available with default trail or line renderer.

User Guide

Your first trail

  1. Create an empty GameObject (Ctrl+Shift+N) and name it.
  2. Drag the SplineTrailRenderer script (located in SplineTrailRenderer/Scripts) on the desired GameObject, or add it with the Add Component button.
  3. Add a MeshFilter and a MeshRenderer as components (In the Component->Mesh menu).
  4. Assign the FadeTrailMaterial material (located in SplineTrailRenderer/Samples/Materials) to the mesh renderer's material list. We use this material for the purpose of demonstration, but of course you can create your own material if you need specific texture or shader for your trail.
  5. Use the same values as in the image below to have a nicer looking trail. Each parameters are explained in detail later in this user guide.

The added components should look like this in the inspector:


When the GameObject will move, a smooth trail will be automatically generated behind it.

Moving objects on the trail


First, let's get the length of the trail.

float length = trailReference.spline.Length();

We increment a member variable "distance", that represents the distance of the player from the beginning of the trail. We make sure it is comprised within the trail boundaries. The tangent at the extremum of the trail is often imprecise, so we stop moving before the end.

distance = Mathf.Clamp(distance + speed * Time.deltaTime, 0, length-0.1f);

We adjust the maximum length of the trail to draw it from the position of the player to the end of the trail.

trailReference.maxLength = Mathf.Max(length - distance, 0);

Get the forward vector and the position of the player at the specified distance.

Vector3 forward = trailReference.spline.FindTangentFromDistance(distance);
Vector3 position = trailReference.spline.FindPositionFromDistance(distance);

Moves and orients our object accordingly to the tangent and position at the given distance on the trail. At the extremums the tangent can be invalid (Vector3.zero), so we do not assign that value to our object in that case.

if(forward != Vector3.zero)
{
    transform.forward = forward;
    transform.position = position;
}

You can see this code in action in the PlayerPath sample located in SplineTrailRenderer/Samples/Scenes. The script Player.cs implements this behavior.

Overview of properties


Emit

Defines whether or not the trail generation is enabled. A common usage would be to emit when the left mouse button is pressed to draw a line, and stop emitting when the button is released to stop drawing.

Emission Distance

When emitting, a newly added point is not locked in place until it is at least at "Emission Distance" from two previous emission point. This is to restrict the control over the drawing of the line to avoid clutters and also to reduce the number of control points on the spline. This value is in world units, and should be tweaked to fit the scale of your game.

Trail on the left has a value of 2, the trail on the right a value of  0.5

Quad Height

Height of quads forming the trail. This actually is the width of the trail.

Quad Width

Width of quads forming the trail. The smaller this value, the more quad there is. 

Trail on the left has a width of 0.5, the trail on the right a width of  0.1

Vertex Color

Constant RGBA color value used for every vertex of the trail. Modifiable at runtime.

Normal

The direction towards which the trail is facing. For example, the trail can appear to be traced on the ground if normal is facing upward (0, 1, 0). If value is (0, 0, 0), the trail will face the MainCamera.

Mesh Disposition

The way mesh is generated on the spline. There is currently two modes, Continuous and Fragmented.

  • Continuous: Quads are all connected together seamlessly, and they may be bent in curves. Texture will be tiled nicely along the trail, independently of the quads width.
  • Fragmented: Quads are never bent, so there may be seams between them. Texture is applied over the surface of one quad, so defining non-square quads will stretch the texture. This mode is used mainly for dotted lines.

Continuous (left) vs Fragmented (right)

Fade Type

The way the trail will appear and disappear. There are four fading types.

  • None: Trail will end abruptly.
  • MeshShrinking:  Bends the trail endpoints to form pointy ends.
  • Alpha: Fade to transparency. Requires a material that support it.
  • Both: Fade to transparency plus shrinking into pointy ends.

None (top left), MeshShrinking (bottom left), Alpha (top right), Both (bottom right)


Fade Length Begin/End

Determine the length of the fade at the beginning and end of the trail, from completely visible, fading linearly to completely invisible.

Max Length

The maximum length of the trail, including fading endpoints. This can of course be modified at run-time to create custom fading behaviors.