Touched Event for Trails

I ran into a minor problem, the trails. I am using Roblox’s trails (because the flexibility, it can make curves) however there is no touched event for that so I am using a “hacky” way making invisible parts that follow my bike and I have a .Touched on those, it’s very bad for performance and I don’t know what I should do. I can’t make a feature suggestion thread because I am not a full member.

You use your trail to kill other people (like slither io)

2 Likes

Listening for touched on many parts could cause lag, as could creating and destroying many parts.

I’d say have a fixed resolution for the trail collision, such as ~6 segments, then you only need to create 6 parts when the bike is created, and just update their CFrame and Size. You’d need to track 6 points trailing behind the bike, by using coroutines and different delays.

For example:

local SegmentVerts = {}
local Resolution = 6
local TrailLifetime = 3--replace this with the Trail life time property
while true do
    coroutine.resume(coroutine.create(
    --in parallel run the following function
        function()
            for i=1, Resolution do
                wait(TrailLifetime/Resolution)
                SegmentVerts[i] = BIKEPOSITIONHERE
            end
        end
    ))
wait()--avoid infinite loop freeze
end

The above code would give you a table with 6 vector3’s each tracking the bikes position some amount of time ago. This would give you something like the picture:
image
Then you can use the 6 premade parts to join up the vertices by using some function for each vertice pair that you put in one of the premade parts into:

function JoinVerts(Part, Pos1, Pos2)
    --CFrame(Vec3 A,Vec3 B) CF at A looking towards B
    local Length = (Pos2-Pos1).Magnitude
    Part.Size = Vector3.new(.2,4,Length) --Trail is 4 studs wide I think
    Part.CFrame = CFrame.new(Pos1,Pos2)
end

This code applied would give you a result something like the next image:
image
And then you’d just need to join the last vertex to the bike position, or you could have 7 verts and start the for loop with i = 0!

5 Likes