I've been sitting here for an hour trying to figure out a simple weapon trail, but each time I try it looks messy. I've tried using triangles and I've tried using regular parts (at the tip of the blade, as an example) and so far I've gotten nothing but messy results. I've scrapped each attempt, so I'd like to ask what you guys would do to solve this problem. The math, more specifically, is what I'm having problems with. Any suggestions?
In this example, I will be using a ServerScript and the wait()
function, however you will get nicer results if you bind it to a render-step.
If you want to make a trail for an object, you need to measure its displacement. Displacement is a vector quantity defined as the change in distance in a particular amount of time. Therefore, in our time loop, we want to keep track of what the previous position of the part was:
local part = game.Workspace.Part while true do local prevPosition = part.Position wait() local displacement = part.Position - prevPosition trail(part, displacement) end
Now we can create our trail. I will make it just a thin block that originates from the center of the object. I will separate the creation out into a function for readability:
function trail(object, displacement) -- Important aspects local trail = Instance.new("Part") trail.Size = Vector3.new(0, 0, displacement.magnitude) trail.CFrame = CFrame.new(object.Position - displacement/2, object.Position) trail.Parent = game.Workspace trail.Anchored = true trail.CanCollide = false game.Debris:AddItem(trail, 3) end
Notice that for the size I used the magnitude of the displacement. This is because the distance of a vector is it's magnitude (distance = magnitude). Also note how I set the CFrame. That particular constuctor of CFrame takes two parameters, the position you want it to be centered and a position you want it to face. I center it at halfway between the previous position and the current position (current position - half displacement) and point it towards the current position.
One additional things to note about this example. You may want to make sure that there is a displacement. If the displacement of the object is very, very small (less than some epsilon) then you probably don't want a trail to be generated. This can be implemented like so:
if displacement.magnitude > 0.01 then trail(parameters) else -- do nothing end