Hi I’m making a shotgun and I’ve been stuck with this issue of the bulletrays not originating from the correct position (shootpart) for a long time now. My intention is for the rays to emerge from the end of the barrel (bead) of the shotgun aka the basepart you see at the location. However, the rays emerge from midair.
Alternative: robloxapp-20240323-1511045.wmv (1.7 MB)
Hierachy:
Code for raycast and bulletspawning:
for i = 1, Projectiles do
task.spawn(function()
local direction = CFrame.new(Shotgun.ShootPart.Position,hitpos)*CFrame.Angles(math.rad(rnd:NextNumber(-Spread, Spread)), math.rad(rnd:NextNumber(-Spread, Spread)), 0 )
local ray = workspace:Raycast(Shotgun.ShootPart.Position,direction.LookVector * MaxRange, Params)
local intersection = ray.Position
local traveldistance = (Shotgun.ShootPart.Position - intersection).Magnitude
local bulletray = game.ServerStorage.Misc.BulletRay:Clone()
bulletray.CFrame = CFrame.lookAt(Shotgun.ShootPart.Position, intersection) *CFrame.new(0, 0, -traveldistance/2)
bulletray.Size = Vector3.new(0.05, 0.05, traveldistance)
Debris:AddItem(bulletray, 0.15)
bulletray.Parent = workspace
end)
end