Issue with GunFirePoint visual Bullet Trail

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.

image

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
1 Like

I forgot to mention I’m firing from an attachment.

If someone is able to solve this, please do so. It hurts getting ignored x_x

if you are firing from an attachment then you can have the attachment0 be where the bullet comes out from and attachment1 where the mouse was clicked (using mouse.Hit.p) and it would be MUCH easier in my opinion to use a beam for this ( perhaps have the beam be a part of attachment0 and then set it’s attachment1 to attachment1 whenever the gun is fired? hope that made sense)

I can’t write out the code for you since I’m not the best myself but I hope this can bring some clarity or inspire you to try something out!

I’m not using Mouse.Hit.P. If you read the title and what I wanted clearly, you’d understand how this would work (sorry if this sounded rude, not intentional)

So basically I’m trying to make it so an object in the game (preferably an NPC of sorts) that can shoot bullets at enemies. I don’t just want a sound and them facing them to give the feel of them being shot it, because there’s no bullet trail and it looks bad without it.

I already have a gun that works perfectly fine with this method and raycasts. Just not with a Humanoid in the game that’s not a player, and that doesn’t have a mouse.

Anyone else reading this that knows about raycasts, please I need your help

You may want to use the WorldPosition of the attachment instead of the position where you set the CFrame of the gun part.

I would do this.

local Midpoint = (TargetPosition + GunFirePoint.WorldPosition) / 2
BulletTrail.CFrame = CFrame.lookAt(Midpoint, GunFirePoint.WorldPosition) 
1 Like

I will let you know if your posted solution will work. I’m just working on something else at the moment.

This actually fixed the issue! I didn’t realize that I had to use + instead of -.

Thank you! I hope this helps others as well.

1 Like