rpascal
rpascal

Reputation: 753

Unity Tron like trail Particle System

I am trying to mimic a trail behind my player similar to in tron. I am currently using a Particle System to create this trail and it works alright. I want this trail to be able to collide with other objects and then I will destroy the objects on collision. I have the collision working but when the object collides with my trail some of the particles move out of line with the rest of the trail. Is there a way to detect collision but not have the particles be affected by other objects?

Particles not aligned after collision

Here are my settings for my particle system, if anyone has any recommendations on how I could improve the way i am doing this that would be awesome. Right now i am just rendering a bunch of circles on top of each other but i am assuming there has to be a better way.

Particle System Properties

Ideally in the long run i would like to be able to make something like this for the trail.

Tron Trail

Thank you!

Upvotes: 2

Views: 1329

Answers (1)

Kadir
Kadir

Reputation: 21

/After thousands of years of searching I came up with this, Generating Part was copied from that guy: https://www.youtube.com/watch?v=BIKYK8qbdG0&t=4s shaderGraph can be use to make the trail material glow/

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TrailMeshGenerator : MonoBehaviour
{
[SerializeField] Transform followPos;    
[SerializeField] float height = 1f;
[SerializeField] float width = 0.001f;  
[SerializeField] float destroyTrailAfterSecond = 4f;  
[SerializeField] Material trailMaterial;    
[SerializeField] float  spawnDelay = 0.01f;


bool firstTime = true;
GameObject trail;
MeshFilter meshFilter;
MeshCollider meshCollider;
List<Vector3> verticesDef;
List<int> triangleDef;
bool isEven = true;
int x;
float currentSpawnTrailTime = 0;


const string addMeshstrName = "AddMesh";

public void SetFollowPos(Transform pos)
{
    followPos = pos;
}

void Start()
{
    InvokeRepeating(addMeshstrName, 0, spawnDelay);
}


private void AddMesh()
{
    MonoLine();
    if (currentSpawnTrailTime > destroyTrailAfterSecond)
    {
        RemoveMesh();
    }

}

void Update()
{
    currentSpawnTrailTime += Time.deltaTime;
}

private void MonoLine()
{
    Vector3[] vertices = null;
    int[] triangles = null;

    var backward = followPos.transform.position - (followPos.transform.forward);

    if (firstTime)
    {
        vertices = new Vector3[]
        {
            backward + (followPos.transform.right * -width),
            backward - (followPos.transform.right * -width),
            backward - (followPos.transform.right * -width) + followPos.transform.up * height,
            backward + (followPos.transform.right * -width) + followPos.transform.up * height,
        };

        triangles = new int[]
        {
            0, 2, 1,
            0, 3, 2,
        };

        trail = new GameObject();
        trail.tag = "Head Trails";
        trail.layer = LayerMask.NameToLayer("Trail");

        meshFilter = trail.AddComponent<MeshFilter>();
        trail.AddComponent<MeshRenderer>().material = trailMaterial;

        meshCollider = trail.AddComponent<MeshCollider>();
        meshCollider.sharedMesh = meshFilter.mesh;

        verticesDef = new List<Vector3>();
        triangleDef = new List<int>();

        foreach (var v in vertices)
            verticesDef.Add(v);
        foreach (var t in triangles)
            triangleDef.Add(t);

        meshFilter.mesh.vertices = vertices;
        meshFilter.mesh.triangles = triangles;

        isEven = false;
        firstTime = false;

        x = 4;
        return;

    }

    if (isEven)
    {
        verticesDef.Add(backward + (followPos.transform.right * -width));
        verticesDef.Add(backward - (followPos.transform.right * -width));
        verticesDef.Add(backward - (followPos.transform.right * -width) + followPos.transform.up * height);
        verticesDef.Add(backward + (followPos.transform.right * -width) + followPos.transform.up * height);

        //left face
        triangleDef.Add(x - 4); //0
        triangleDef.Add(x - 1); //3
        triangleDef.Add(x);     //4

        triangleDef.Add(x - 4); //0
        triangleDef.Add(x);     //4
        triangleDef.Add(x + 3); //7

        //top face
        triangleDef.Add(x - 4); //0
        triangleDef.Add(x + 3); //7
        triangleDef.Add(x + 2); //6

        triangleDef.Add(x - 4); //0
        triangleDef.Add(x + 2); //6
        triangleDef.Add(x - 3); //1

        //right face
        triangleDef.Add(x - 3); //5
        triangleDef.Add(x + 2); //10
        triangleDef.Add(x + 1); //9

        triangleDef.Add(x - 3); //5
        triangleDef.Add(x + 1); //9
        triangleDef.Add(x - 2); //6

        isEven = false;

    }
    else
    {
        verticesDef.Add(backward + (followPos.transform.right * -width) + followPos.transform.up * height);
        verticesDef.Add(backward - (followPos.transform.right * -width) + followPos.transform.up * height);
        verticesDef.Add(backward - (followPos.transform.right * -width));
        verticesDef.Add(backward + (followPos.transform.right * -width));

        //left face
        triangleDef.Add(x - 4); //0
        triangleDef.Add(x + 3); //7
        triangleDef.Add(x);     //4

        triangleDef.Add(x - 4); //0
        triangleDef.Add(x);     //4
        triangleDef.Add(x - 1); //3

        //top face
        triangleDef.Add(x - 2); //2
        triangleDef.Add(x - 1); //3
        triangleDef.Add(x);     //4

        triangleDef.Add(x - 2); //2
        triangleDef.Add(x);     //4
        triangleDef.Add(x + 1); //5

        //right face
        triangleDef.Add(x - 3); //5
        triangleDef.Add(x - 2); //6
        triangleDef.Add(x + 1); //9

        triangleDef.Add(x - 3); //5
        triangleDef.Add(x + 1); //9
        triangleDef.Add(x + 2); //10

        isEven = true;
    }
    x += 4;
    
    meshFilter.mesh.vertices = verticesDef.ToArray();
    meshFilter.mesh.triangles = triangleDef.ToArray();

    meshCollider.sharedMesh = meshFilter.mesh;

   // Debug.Log(x +" bounds->" +meshFilter.mesh.bounds.size.magnitude);
    
}

private void RemoveMesh()
{
    verticesDef.RemoveAt(0);     
    verticesDef.RemoveAt(0);     
    verticesDef.RemoveAt(0);     
    verticesDef.RemoveAt(0);

    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);

    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);

    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);

    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);

    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);

    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);
    triangleDef.RemoveAt(triangleDef.Count - 1);

    meshFilter.mesh.Clear();

    meshFilter.mesh.vertices = verticesDef.ToArray();
    meshFilter.mesh.triangles = triangleDef.ToArray();

    x -= 4;

}
}

Upvotes: 0

Related Questions