Hello!
I am trying to make a bullet trail for my gun. It uses fastcast and I got it to work kinda.
There is a large gap in-between the barrel and the actual bullet trail though. How would I fix this.
Here’s my code:
The bullet template is the thing that fires.
--Vars
local Tool = script.Parent.Parent
local FireEvent = Tool.Events:WaitForChild("FireEvent")
local FastCast = require(Tool.FastCastRedux)
local FirePoint
local Values = Tool.Values
local BulletsFolder = game.Workspace:WaitForChild("BulletsFolder")
local PartCache = require(Tool.PartCache)
--FastCast.VisualizeCasts = true
local Caster = FastCast.new()
--Important Vars
local BulletSpeed = Values.BulletPhysics.BulletSpeed.Value
local BulletDrop = Values.BulletPhysics.BulletDrop.Value
local HeadDMG = Values.DMG.HeadDMG.Value
local BodyDMG = Values.DMG.BodyDMG.Value
local ArmDMG = Values.DMG.ArmDMG.Value
local LegDMG = Values.DMG.LegDMG.Value
local ExtremityDMG = Values.DMG.ExtremityDMG.Value
--Bullet Template
local BulletTemplate = Instance.new("Part")
BulletTemplate.Anchored = true
BulletTemplate.CanCollide = false
BulletTemplate.Shape = "Ball"
BulletTemplate.Size = Vector3.new(0.08, 0.08, 0.08)
BulletTemplate.Material = Enum.Material.Metal
BulletTemplate.Color = Color3.fromRGB(255, 255, 0)
BulletTemplate.Transparency = 1
local attachment0 = Instance.new("Attachment")
attachment0.Name = "Attachment0"
attachment0.Parent = BulletTemplate
attachment0.Position = Vector3.new(0, 0.4, 4)
local attachment1 = Instance.new("Attachment")
attachment1.Name = "Attachment1"
attachment1.Parent = BulletTemplate
attachment1.Position = Vector3.new(0, -0.4, 4)
local trail = Instance.new("Trail")
trail.Attachment0 = attachment0
trail.Attachment1 = attachment1
trail.Parent = BulletTemplate
color = Color3.fromRGB(255, 255, 0)
trail.Color = ColorSequence.new(color)
trail.FaceCamera = true
trail.Lifetime = 0.001
trail.Name = "Trail"
trail.Brightness = 1000
trail.LightEmission = 1000
trail.MaxLength = 1
trail.Transparency = NumberSequence.new(0)
trail.LightInfluence = 1000
trail.Enabled = false
--Cast Behaviour
local CastParams = RaycastParams.new()
CastParams.FilterType = Enum.RaycastFilterType.Blacklist
CastParams.IgnoreWater = false
local BulletCache = PartCache.new(BulletTemplate, 1000, BulletsFolder)
local CastBehaviour = FastCast.newBehavior()
CastBehaviour.RaycastParams = CastParams
CastBehaviour.Acceleration = Vector3.new(0, BulletDrop, 0)
CastBehaviour.AutoIgnoreContainer = false
CastBehaviour.MaxDistance = 10000
CastBehaviour.CosmeticBulletProvider = BulletCache
CastBehaviour.CosmeticBulletContainer = BulletsFolder
--OnEquipped Function
local function OnEquipped()
CastParams.FilterDescendantsInstances = {Tool.Parent, BulletsFolder}
end
--LengthCHanged Function
local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
if bullet then
local bulletLength = bullet.Size.Z/2
local offset = CFrame.new(0, 0, -(length - bulletLength))
bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction)
wait(0)
bullet.Trail.Enabled = true
end
end
--onRayHit Function/Damage and destroy Bullet function
local function onRayHit(cast, result, veloticy, bullet)
local hit = result.Instance
local char = hit:FindFirstAncestorWhichIsA("Model")
if char and char:FindFirstChild("Humanoid") then
char.Humanoid:TakeDamage(33)
end
delay(0, function()
bullet.Trail.Enabled = false
BulletCache:ReturnPart(bullet)
end)
end
--Fire Function
local function Fire(player, mousePos, char)
FirePoint = char.Head
local origin = FirePoint.Position
local direction = (mousePos - origin).Unit
Caster:Fire(origin, direction, BulletSpeed, CastBehaviour)
end
--Connects
FireEvent.OnServerEvent:Connect(Fire)
Tool.Equipped:Connect(OnEquipped)
--Caster
Caster.LengthChanged:Connect(onLengthChanged)
Caster.RayHit:Connect(onRayHit)
Heres a Video of what happen
Fullscreen the video to see the bullet trail