How to add a collider to a trail renderer.

I have to objects that move independently and they each have a trail behind them. I want it so that they cannot go through each others trail. But each object can go through its own trail. Eventually I would like to make it so that when one object collides with the other objects trail, the game ends. But I would really just like to know how to add a collider to the trail. Thanks!

This is a bit late, but I wrote a script that creates trail renderer with a collider for a game that I worked on. You can find it on the Unity wiki:

Sounds like tron :). I’ve made a pretty simple version of it, so here’s the advice I have to offer:

1: Adding a collider to a trail renderer is pretty difficult stuff. I was able to get by by simply creating invisible boxes with colliders behind the moving object. Later, to improve performance, you can make it so the colliders are long and far in between, and make them shorter and more often when banking a turn.

2: Layers, Layers Layers!! You would want to put your moving object and it’s trail on the same layer (next to tag). Then, in any script, you can adjust which layers will collide with each other. For example:
Physics.IgnoreLayerCollision (0, 0);
will make the default layer unable to collide with itself.

  1. Syncopate your trail renderer with the actual trail (make them have the same length). This may sound easy, but it was the hardest part for me. What I ended up doing was attach a self destruction script to my trail collider prefab. It went something like this:
    public float lifespan;
    private float timer;
    //Create this later:
    public AnotherScript master;

    void Update () {
    //Set the lifespan of this cube to the masterscript length value
    lifespan = master.length;
    //Keep the timer ticking!
    timer += Time.deltaTime;
    //If the timer (which keeps track of how many seconds we’ve been alive) is bigger than our allotted lifespan, destroy ourselves
    GameObject.Destroy (gameObject);

Then, you would attach the masterscript to the trail renderer and make it also manage the length of the trail withGetComponent().time=length
Let me know if you have any questions!

Hi, I come from the future. We are not doing so good (long story), but I can at least make life easier for people that are searching for ways to implement this behaviour (as I found this page googling for solutions).

Trail Renderer has a method called TrailRenderer.BakeMesh that lets you take a “snapshot” of the mesh it created and store it in another mesh (you provide as a parameter).

Now, I was working on a mobile 2D game that lets the player swipe the screen, drawing a short line, that can then interact with the
game world, but I learnt that mesh colliders (which sounds like the most obvious next step) can’t interact with 2d rigidbody/colliders, so I had to create a PolygonCollider2D, change its “points” (an array of Vector2Ds that contains the polygon vertices) to match the vertices of the mesh (after a little conversion from an array of Vector2s to Vector3s), everything attached to a Game Object I create when dereferenciated and dereferenciate when the swipe ends (so to have only 1 polygon per swipe but make it so there can be more then 1 swipe at any given type).

This Game Object also needs a Rigidbody2D, obviously, and you can attach to it a MeshFilter+MeshRenderer so you can clearly see it (even after the trail renderer has disappeared, if you want the effect of a sweeping color and another one below it).

The last thing: remember to check that the Trail Renderer doesn’t have “Min Vertex Distance” too small and too many" End Cap Vertices" or you could get weird collision errors, or none at all.

Here is the code (I call the DrawSwipe every frame there are points in the swipe, which is a stack):

    public void DrawSwipe(TrailRenderer trail)
        if (trail!= null)
            Mesh mesh = new Mesh();
            trail.BakeMesh(mesh, Camera.main, true);
            SwipeObject.GetComponent<MeshFilter>().mesh = mesh;

            PolygonCollider2D collider = SwipeObject.GetComponent<PolygonCollider2D>();
            List<Vector2> verts = new List<Vector2>();
            foreach (Vector2 vertex in mesh.vertices)

            collider.points = verts.ToArray();

This is the GameObject that actually collides with stuff (it’s a property):

    private GameObject swipeObject;
    private GameObject SwipeObject
        set => swipeObject = value;

            if (swipeObject== null)
                swipeObject = new GameObject("Swipe", typeof(PolygonCollider2D), typeof(Rigidbody2D), typeof(MeshFilter), typeof(MeshRenderer));
                swipeObject.GetComponent<Rigidbody2D>().isKinematic = true;
                swipeObject.GetComponent<MeshRenderer>().material = material;
            return swipeObject;