Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


TrailRenderer

Creates a visual trail of polygons that follows a GameObject's movement, useful for emphasizing motion or creating dynamic visual effects.
Read time 12 minutesLast updated 5 days ago

Definition

public sealed class TrailRenderer : Renderer

Remarks

Use the Trail Renderer to tie an effect to a moving GameObject’s position. Trail Renderers help create visual effects such as missile trails, weapon swing arcs, or highlighting the path of fast-moving objects. They provide a dynamic way to visually emphasize motion, making them useful for effects in action-oriented games or scenes with rapid object movement.
The component works by storing the GameObject’s position at intervals defined by the TrailRenderer.minVertexDistance property and connecting these points to form a continuous ribbon-like trail. You can control the duration of the trail using the TrailRenderer.time property, and you can configure its appearance, including width and color gradients, using a Material.
To control the trail effect, use the TrailRenderer.Clear method. This method instantly removes any existing trail segments and prevents the Trail Renderer from adding new points.
The following code sample demonstrates how to attach a Trail Renderer to a moving GameObject and configure some of its basic properties. Attach this script to an empty GameObject in your scene.

Examples

// Ensure the GameObject has a TrailRenderer componentusing UnityEngine;[RequireComponent(typeof(TrailRenderer))]public class SimpleTrail : MonoBehaviour{ public float moveSpeed = 3.0f; public float circleRadius = 2.0f; private TrailRenderer trail; private float angle = 0f; void Start() { trail = GetComponent<TrailRenderer>(); // Configure the trail's appearance trail.time = 1.5f; // How long the trail lasts in seconds trail.startWidth = 0.3f; // Width at the newest part of the trail trail.endWidth = 0f; // Width at the oldest part of the trail // Set a color gradient (e.g., white fading to transparent red) Gradient gradient = new Gradient(); gradient.SetKeys( // Color keys: White at the start, Red at the end new GradientColorKey[] { new GradientColorKey(Color.white, 0.0f), new GradientColorKey(Color.red, 1.0f) }, // Alpha keys: Opaque at the start, Transparent at the end new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f), new GradientAlphaKey(0.0f, 1.0f) } ); trail.colorGradient = gradient; GameObject head = GameObject.CreatePrimitive(PrimitiveType.Sphere); Transform headTransform = head.GetComponent<Transform>(); headTransform.SetParent(transform); headTransform.localPosition = Vector3.zero; headTransform.localScale = Vector3.one * 0.2f; } void Update() { // Move the GameObject in a circle angle += moveSpeed * Time.deltaTime; float x = Mathf.Cos(angle) * circleRadius; float z = Mathf.Sin(angle) * circleRadius; transform.position = new Vector3(x, 0, z); }}

Properties

Property

Description

alignmentSelect whether the trail will face the camera, or the orientation of the Transform Component.
applyActiveColorSpaceSet whether colors will be converted appropriately before being passed to the GPU when using Linear Rendering.
autodestructDoes the GameObject of this Trail Renderer auto destruct?
colorGradientSet the color gradient describing the color of the trail at various points along its length.
emittingCreates trails when the GameObject moves.
endColorSet the color at the end of the trail.
endWidthThe width of the trail at the end of the trail.
generateLightingDataConfigures a trail to generate Normals and Tangents. With this data, Scene lighting can affect the trail via Normal Maps and the Unity Standard Shader, or your own custom-built Shaders.
maskInteractionSpecifies how the TrailRenderer interacts with SpriteMask.
minVertexDistanceSet the minimum distance the trail can travel before a new vertex is added to it.
numCapVerticesSet this to a value greater than 0, to get rounded corners on each end of the trail.
numCornerVerticesSet this to a value greater than 0, to get rounded corners between each segment of the trail.
positionCountGet the number of line segments in the trail.
shadowBiasApply a shadow bias to prevent self-shadowing artifacts. The specified value is the proportion of the trail width at each segment.
startColorSet the color at the start of the trail.
startWidthThe width of the trail at the spawning point.
textureModeChoose whether the U coordinate of the trail texture is tiled or stretched.
textureScaleA multiplier for the UV coordinates of the trail texture.
timeHow long does the trail take to fade out.
widthCurveSet the curve describing the width of the trail at various points along its length.
widthMultiplierSet an overall multiplier that is applied to the TrailRenderer.widthCurve to get the final width of the trail.

Methods

Method

Description

AddPositionAdds a position to the trail.
AddPositionsAdd an array of positions to the trail.
BakeMeshCreates a snapshot of TrailRenderer and stores it in
mesh
.
ClearRemoves all points from the TrailRenderer. Useful for restarting a trail from a new position.
GetPositionGet the position of a vertex in the trail.
GetPositionsGet the positions of all vertices in the trail.
GetVisiblePositionsGet the visible positions of all vertices in the trail.
SetPositionSet the position of a vertex in the trail.
SetPositionsSets the positions of all vertices in the trail.

Inheritance

Inherited Members

Operators

Operator

Description

operator ==Compares two object references to see if they refer to the same object.
boolDetermines whether the object exists.
operator !=Compares if two objects refer to a different object.