New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Raycast Gun Accuracy Help?

Asked by 4 years ago

Hey, guys. So I read a little about the Roblox Wiki article about raycast weapons, and I made the following script that makes the gun shoot. It is working fine:

local tool = script.Parent
Player = tool.Parent.Parent
local user
fire = script.Parent.Handle.Fire
Shooting = false

tool.Equipped:connect(function(mouse)
    user = tool.Parent 
    mouse.Button1Down:connect(function()
        if tool.Ammo.Value > 0 then
            function shoot()
                Shooting = true
                local offset = {0.2,0.5,0,7}
                local numnum = offset[math.random(1, #offset)]

                local ray = Ray.new(tool.Hole.CFrame.p, (mouse.Hit.p - tool.Hole.CFrame.p).unit*300)
                local hit, position = game.Workspace:FindPartOnRay(ray, user)

                local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
                if humanoid then
                    humanoid:TakeDamage(20)
                end

                local distance = (position - tool.Hole.CFrame.p).magnitude
                local rayPart = Instance.new("Part", user)
                rayPart.Name          = "RayPart"
                rayPart.BrickColor    = BrickColor.new("Bright yellow")
                rayPart.Transparency  = 0
                rayPart.Anchored      = true
                rayPart.CanCollide    = false
                rayPart.TopSurface    = Enum.SurfaceType.Smooth
                rayPart.BottomSurface = Enum.SurfaceType.Smooth
                rayPart.formFactor    = Enum.FormFactor.Custom
                rayPart.Size          = Vector3.new(0.2, 0.2, distance)
                rayPart.CFrame        = CFrame.new(position, tool.Hole.CFrame.p) * CFrame.new(0, 0, -distance/2)
                tool.Ammo.Value = tool.Ammo.Value - 1
                fire:Play()
                game.Debris:AddItem(rayPart, 0.001)
                wait(0.08)
            end
            repeat shoot() until not Shooting or tool.Ammo.Value <= 0
        end
    end)
    mouse.Button1Up:connect(function()
        Shooting = false
    end)
end)

What I want to do now is make the gun have some accuracy! To do this, I want to multiply the ray's CFrame by a small random number so that it will be slightly offseted from the player's mouse. Notice how in lines 13 + 14, I made a variable called numnum that randomly chooses a number out of the table called offset. NOW HERE'S THE PROBLEM!!: Where do I multiply this number to?... I'd gladly appreciate it if someone could help me solve this problem. Thank You! Oh! BTW, Hole - mentioned in line 16 - is where the ray originates from (the barrel of the gun). It is a part.

0
While setting the CFrame, make the second argument(Vector3 lookAt point) the Hole's CFrame multiplied by a randomized integer. It will essentially offset the end point, multiply on all axises for realistic offset. Goulstem 8062 — 4y
0
Um... Can you give more detailed information? I'm still confused. dpark19285 370 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

A few things:

You would add it on to the ray defining line, in this case, line 16.

Line 16: local ray = Ray.new(tool.Hole.CFrame.p, (mouse.Hit.p + numnum - tool.Hole.CFrame.p).unit*300)

However, there are problems with this solution. First off all, make sure that you are considering negative values in your variance. At the moment the rays will only fire towards the top right. Secondly, this solution is flawed because your gun will have the same accuracy if you are on the other side of the map as it would if you were point blank. Believe me, I am having the same problem. If you can find a better way to fix this, please let me know, and if I find a better way, (I was kinda thinking about taking the X value of the ray into account) I will make sure to tell you. Happy coding!

0
Yes, thank you, that also works! But I also figured out another way to do it myself. Thank you so much for helping! dpark19285 370 — 4y
Ad

Answer this question