generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Add a "Spread" with RayCasting?

Previous Thread :: Next Thread 
MrMcAero is not online. MrMcAero
Joined: 21 Apr 2012
Total Posts: 671
29 May 2012 03:42 PM
So, I got the Raygun script from the Wiki, and how could I add a Spread to the bullet? Only at the end of it though, I tried but I couldn't. Here's what I came up with:

local Tool = script.Parent
local User
local Spread = 10
local Automatic = false --Set to false for semi.
local Firing = false
local FireRate = 0.075

Tool.Equipped:connect(function(mouse)
   User = Tool.Parent
    function Fire()
    local Ray = Ray.new(Tool.Tip.CFrame.p,((mouse.Hit.p-Tool.Tip.CFrame.p)*math.random(0,Spread)).unit*300) --Here.
   local Hit,Position = game.Workspace:FindPartOnRay(Ray,User)
        if Hit then
            if Hit.Parent:FindFirstChild("Humanoid") then
                Hit.Parent.Humanoid:TakeDamage(20)
            end
        end
   local RayPart = Instance.new("Part",User)
    RayPart.Name = "RayPart"
    RayPart.BrickColor = BrickColor.new("Bright red")
    RayPart.Transparency = 0.5
    RayPart.Anchored = true
    RayPart.CanCollide = false
    RayPart.TopSurface = Enum.SurfaceType.Smooth
    RayPart.BottomSurface = Enum.SurfaceType.Smooth
    RayPart.formFactor = Enum.FormFactor.Custom
    local Distance = (Position-Tool.Tip.CFrame.p).magnitude
    RayPart.Size = Vector3.new(0.2,0.2,Distance)
    RayPart.CFrame = CFrame.new(Position,Tool.Tip.CFrame.p) * CFrame.new(0,0,-Distance/2)
    game.Debris:AddItem(RayPart,0.1)
    Tool.Handle.PewPew:Play()
    end
   mouse.Button1Down:connect(function()
        if Automatic then
            Firing = true
            repeat Fire() wait(FireRate) until Firing == false
        else
            Fire()
        end
   end)
   mouse.Button1Up:connect(function()
        Firing = false
   end)
end)

MrMcAero
Report Abuse
FinalNight is not online. FinalNight
Joined: 22 Mar 2011
Total Posts: 379
29 May 2012 03:43 PM
"Spread" meaning what? Having the bullet move farther from the place you originally clicked as distance increases?

--(Support The Tributes)-{http://www.roblox.com/--place?id=71037485}--
Report Abuse
MrMcAero is not online. MrMcAero
Joined: 21 Apr 2012
Total Posts: 671
29 May 2012 03:44 PM
Yea, like if you look at a real gun, it never actually hits where you point at. So, the end point of the bullet has an "Offset", and you can set that offset.

MrMcAero
Report Abuse
malachi11 is not online. malachi11
Joined: 07 May 2008
Total Posts: 2420
29 May 2012 03:50 PM
local Ray = Ray.new(Tool.Tip.CFrame.p,((mouse.Hit.p+Vector3.new(math.random(0,Spread),math.random(0,Spread),math.random(0,Spread))-Tool.Tip.CFrame.p).unit*300)


Try that. :P
Report Abuse
FinalNight is not online. FinalNight
Joined: 22 Mar 2011
Total Posts: 379
29 May 2012 03:51 PM
--> RayPart.CFrame = CFrame.new(Position,Tool.Tip.CFrame.p) * CFrame.new(0,0,-Distance/2)<--

I think that if you use a math.random() for the CFrame that it's multiplying by, it will most likely create a spread.
Report Abuse
FinalNight is not online. FinalNight
Joined: 22 Mar 2011
Total Posts: 379
29 May 2012 03:51 PM
@Mal:
Darn you. :P
I had to take out the garbage. :P
Report Abuse
MrMcAero is not online. MrMcAero
Joined: 21 Apr 2012
Total Posts: 671
29 May 2012 03:51 PM
Testing..

Outcome:

Doesn't even fire.

MrMcAero
Report Abuse
MrMcAero is not online. MrMcAero
Joined: 21 Apr 2012
Total Posts: 671
29 May 2012 03:54 PM
You were missing a ), but now I get an output:

Players.Player.Backpack.SemiAuto.Shooter:11: bad argument #2 to 'random' (number expected, got nil)

MrMcAero
Report Abuse
MrMcAero is not online. MrMcAero
Joined: 21 Apr 2012
Total Posts: 671
29 May 2012 03:55 PM
Got it. I forgot to define Spread somehow.

MrMcAero
Report Abuse
MrMcAero is not online. MrMcAero
Joined: 21 Apr 2012
Total Posts: 671
29 May 2012 03:56 PM
THANKS!

MrMcAero
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image