Hello!
I made a gun system by using raycasting.
But I am bad at math and don’t know how to make a spread
Can someone help, please?
Source
script.Parent.CastBullet.OnServerEvent:Connect(function(plr, mouse, dmg, FirePoint, char, range)
local ray = Ray.new(FirePoint.WorldCFrame.Position, (mouse.Position - FirePoint.WorldCFrame.Position).Unit * range)
local ignore = workspace.NoRay:GetChildren();
table.insert(ignore, plr.Character)
local part, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore)
local bullet = Instance.new("Part", workspace.NoRay)
bullet.Name = "Bullet"
bullet.Anchored = true
bullet.Material = Enum.Material.Neon
bullet.CanCollide = false
bullet.Locked = true
bullet.BrickColor = BrickColor.new("New Yeller")
local distance = (FirePoint.WorldCFrame.Position - position).Magnitude
bullet.Size = Vector3.new(0.1, 0.1, distance)
bullet.CFrame = CFrame.new(FirePoint.WorldCFrame.Position, position) * CFrame.new(0, 0, - distance / 2)
if part then
local hum = part.Parent:FindFirstChild("Humanoid")
if not hum then
hum = part.Parent.Parent:FindFirstChild("Humanoid")
end
if hum then
hum:TakeDamage(dmg)
if part.Name == "Head" then
hum:TakeDamage(dmg * 2)
end
end
end
game.Debris:AddItem(bullet, 0.1)
end)
To add spread to a raycasting weapon, simply add a math.random() on the finishing position.
local SPREAD = 1000 -- set some value for it
local spreadPosition = Vector3.new(
FirePoint.WorldCFrame.Position.X + math.random(-SPREAD, SPREAD)/1000,
FirePoint.WorldCFrame.Position.Y + math.random(-SPREAD, SPREAD)/1000,
FirePoint.WorldCFrame.Position.Z + math.random(-SPREAD, SPREAD)/1000
)
I reckon there was a better method.
Looks like parent argument of Instance.new() is here again. Do not use it.
This is a bad method for spread. Adjusting based on the mouse position will lead to inaccurate spread based on range. You need to alter a unit direction rather than the final position.