Hi. Keeping this one short and concise so people don’t ignore it. Seems to be a common trend here.
I want to effectively have a bullet trail come from a GunFirePoint (the barrel) to show that it hit the enemy. I was testing a Tower system (no, not making a game out of it) for a friend so I could show them up I would go about doing it. Also to test my own abilities.
Turns out the bullet trail only comes from the GunFirePoint at a specific angle, and anything past that will make it come from the ground, or beside the character.
Any help with this would be nice! Here’s my script:
Script of Issue 📜
-- Services --
local DebrisService = game:GetService("Debris")
-- Useful variables on the Tower provided --
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
-- Configuration for determining range, firerate, and dmg --
local Setup = Character:WaitForChild("Setup")
local Animations = Setup:WaitForChild("Animations")
local Firerate = Setup:WaitForChild("Firerate")
local Range = Setup:WaitForChild("Range")
local Unit_Number = script:GetAttribute("Unit_Number")
-- BodyParts required --
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Handle = Character:WaitForChild("Handle")
local BulletTrailStorage = Character:WaitForChild("BulletTrailStorage")
-- Prepare visual effects --
local RaycastSettings = RaycastParams.new()
RaycastSettings.IgnoreWater = true
local GunFirePoint = Handle:WaitForChild("GunFirePoint")
local BarrelLight = GunFirePoint:WaitForChild("BarrelLight")
local FireEffect = GunFirePoint:WaitForChild("FireEffect")
-- Prepare animations, and load the idle before the loop --
local IdleSource = Animations:WaitForChild("Idle")
local FireSource = Animations:WaitForChild("Fire")
local IdleAnim = Animator:LoadAnimation(IdleSource)
local FireAnim = Animator:LoadAnimation(FireSource)
IdleAnim:Play()
while true do
for _, v in pairs(workspace:GetChildren()) do
if v:FindFirstChildWhichIsA("Humanoid") then
if v == Character then
-- do nothing
else
if v:FindFirstChild("Configuration"):FindFirstChild("CanKill").Value == false then
-- do nothing
else
local FoundHumanoid = v:FindFirstChildWhichIsA("Humanoid")
local FoundRootPart = v:FindFirstChild("HumanoidRootPart")
local DistanceCheck = (HumanoidRootPart.Position - FoundRootPart.Position).Magnitude
local GunDistance = (FoundRootPart.Position - GunFirePoint.WorldPosition).Magnitude
if DistanceCheck < Range.Value then
print("Distance from "..v.Name.." is about "..DistanceCheck.." stud(s)")
Handle.FireSound:Play()
FireAnim:Play()
-- Create Raycast to nearest enemy --
local BulletRay = workspace:Raycast(GunFirePoint.WorldPosition, (FoundRootPart.Position - GunFirePoint.WorldPosition).Unit * Unit_Number, RaycastSettings)
print(BulletRay.Instance)
-- Create the visual for the bullet --
local BulletTrail = Instance.new("Part")
BulletTrail.Material = Enum.Material.Neon
BulletTrail.BrickColor = BrickColor.new("Neon orange")
BulletTrail.CanCollide = false
BulletTrail.CanQuery = false
BulletTrail.Anchored = true
BulletTrail.Massless = true
BulletTrail.CFrame = CFrame.new(FoundRootPart.Position, GunFirePoint.Position) * CFrame.new(0, 0, -GunDistance / 2)
BulletTrail.Size = Vector3.new(0.05, 0.05, GunDistance)
BulletTrail.Name = "BulletTrail"
BulletTrail.Parent = BulletTrailStorage
DebrisService:AddItem(BulletTrail, 0.5)
-- Enable disable lighting effects --
BarrelLight.Enabled = true
FireEffect.Enabled = true
task.wait(0.15)
BarrelLight.Enabled = false
FireEffect.Enabled = false
end
end
end
end
end
task.wait(Firerate.Value)
end