Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I add spread to my raycast pistol?

Asked by
Axenori 124
5 years ago

I've recently made a pistol using raycast, I can't figure out how to add spread.

Local script

01local BulletEvent = script.Parent:WaitForChild("BulletEvent")
02local debounce = false
03script.Parent.Equipped:Connect(function(Mouse)
04 Mouse.Button1Down:Connect(function()
05    if debounce == false then
06        debounce = true
07    script.Parent.BulletEvent:FireServer(script.Parent.BE.CFrame.Position,Mouse.Hit.Position)
08    wait(0.5)
09    debounce = false
10    end
11end)
12    end)

Normal script

01script.Parent.BulletEvent.OnServerEvent:Connect(function(Player, FromP, ToP)
02 local RayCast = Ray.new(FromP,(ToP-FromP).unit*100)
03 local Part,Position,Normal = game.Workspace:FindPartOnRay(RayCast,Player.Character, false,true)
04 local Dist = (ToP-FromP).magnitude
05 if not Dist then Dist = 300 end
06 local Hole = game.ReplicatedStorage:FindFirstChild("GlockBulletHole"):Clone()
07if Part ~= nil then
08 Hole.Parent = Part.Parent
09 Hole.Position = ToP
10 Hole.CFrame = CFrame.new(Hole.Position, Hole.Position+Normal)
11 local Weld = Instance.new("Weld")
12 Weld.Part0 = Part
13 Weld.Part1 = Hole
14 Weld.C0 = Part.CFrame:Inverse()
15 Weld.C1 = Hole.CFrame:Inverse()
View all 36 lines...

1 answer

Log in to vote
0
Answered by 5 years ago

Hello :) I've made my own spread system lately, I'll share it with you

1local spreadLevel = VALUE -- Your recoil level. It goes from 1 (bullets flying everywhere) to 100 (expert aim). You'll have to experiment a little bit
2 
3local spreadPoint= gun.PrimaryPart.CFrame * CFrame.new(-spreadLevel, 0, 0) -- Creating a point in front of the gun.
4local spreadX, spreadY = math.random(-10, 10), math.random(-10, 10) -- Getting random values of X spread and Y spread
5local finalSpread= spreadPoint* CFrame.new(spreadX/10, spreadY /10, 0) -- Calculating a final spread
6 
7-- After this is done, you have to launch a ray in direction of "finalSpread" position.

If this code doesn't work, I recommend switching X and Z coordinate. So, last line would look like this:

1local finalSpread= spreadPoint* CFrame.new(0, spreadY /10, spreadX/10)

That's how I made it. If you're having any trouble, let me know!

Ad

Answer this question