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: Ray Casting

Previous Thread :: Next Thread 
TheDoctor4816 is not online. TheDoctor4816
Joined: 08 Feb 2009
Total Posts: 396
11 Nov 2013 02:40 PM
local ray = Ray.new(tool.Handle.CFrame.p, (tool.Handle.CFrame.p).unit*300)

I have that line, but how would I make the ray go out of the handle straight? e.g the ray should always go straight out the barrel, no matter where the tool is pointing, or where my mouse is.
Report Abuse
Ravenshield is not online. Ravenshield
Joined: 21 Nov 2007
Total Posts: 4143
11 Nov 2013 02:42 PM
I suggest taking a look at http://wiki.roblox.com/index.php/CFrame
and utilizing the "CFrame.lookVector".

lookVector gives the direction pointing forward where the "Front" of the Part is.


local ray = Ray.new(tool.Handle.CFrame.p, (tool.Handle.CFrame.lookVector).unit * 300)
Report Abuse
TheDoctor4816 is not online. TheDoctor4816
Joined: 08 Feb 2009
Total Posts: 396
11 Nov 2013 02:47 PM
Ahh thank you
Report Abuse
TheDoctor4816 is not online. TheDoctor4816
Joined: 08 Feb 2009
Total Posts: 396
11 Nov 2013 03:15 PM
Hmmm. although the ray is in the direction, it just spawns at (0,0,0). I'm not sure why...

Full Script:

local tool = script.Parent
local user

tool.Equipped:connect(function(mouse)
user = tool.Parent
mouse.Button1Down:connect(function()
local ray = Ray.new(tool.Handle.CFrame.p, (tool.Handle.CFrame.lookVector).unit*300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)
local distance = (position - tool.Handle.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
local rayMesh = Instance.new("CylinderMesh", rayPart)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("Bright red")
rayPart.Transparency = 0.3
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.5, distance, 0.5)
rayPart.CFrame = tool.Handle.CFrame * CFrame.new(0, -distance/2, 0)
rayPart.CFrame = CFrame.Angles(0, math.pi/2, 0)
end)
end)

Yes I had to rotate the ray due to cylindermesh's being the wrong way round....
Report Abuse
Inventite is not online. Inventite
Joined: 20 May 2013
Total Posts: 6
11 Nov 2013 03:22 PM
You're not setting its first position.
Report Abuse
TheDoctor4816 is not online. TheDoctor4816
Joined: 08 Feb 2009
Total Posts: 396
11 Nov 2013 03:23 PM
For example?
Report Abuse
Inventite is not online. Inventite
Joined: 20 May 2013
Total Posts: 6
11 Nov 2013 03:25 PM
local rayPart = Instance.new("Part", user)
local rayMesh = Instance.new("CylinderMesh", rayPart)

rayPart.Name = "RayPart"

rayPart.BrickColor = BrickColor.new("Bright red")

rayPart.Transparency = 0.3

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.5, distance, 0.5)

rayPart.CFrame = tool.Handle.CFrame * CFrame.new(0, -distance/2, 0)

rayPart.CFrame = CFrame.Angles(0, math.pi/2, 0)

Set the raypart position to a bit in front of your gun.
Report Abuse
XAXA is not online. XAXA
Joined: 10 Aug 2008
Total Posts: 6315
11 Nov 2013 03:26 PM
local ray = Ray.new(tool.Handle.CFrame.p, (tool.Handle.CFrame.p * CFrame.new(0,0,1).unit*300)

Jumble the CFrame.new(0,0,1) around. I don't know how your gun is oriented.
Report Abuse
XAXA is not online. XAXA
Joined: 10 Aug 2008
Total Posts: 6315
11 Nov 2013 03:28 PM
Whoops.

local ray = Ray.new(tool.Handle.CFrame.p, ((tool.Handle.CFrame * CFrame.new(0,0,1)).p).unit*300)
Report Abuse
TheDoctor4816 is not online. TheDoctor4816
Joined: 08 Feb 2009
Total Posts: 396
11 Nov 2013 03:30 PM
unit is not a valid member.....
Report Abuse
TheDoctor4816 is not online. TheDoctor4816
Joined: 08 Feb 2009
Total Posts: 396
11 Nov 2013 03:30 PM
Sorry, didn't see the correction!
Report Abuse
TheDoctor4816 is not online. TheDoctor4816
Joined: 08 Feb 2009
Total Posts: 396
11 Nov 2013 03:32 PM
Now it's just spawning the ray part miles a way, pointing in the right direction, but at a slight angle....

Full Script:

local tool = script.Parent
local user

tool.Equipped:connect(function(mouse)
user = tool.Parent
mouse.Button1Down:connect(function()
local ray = Ray.new(tool.Handle.CFrame.p, ((tool.Handle.CFrame * CFrame.new(0,0,1)).p).unit*300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)
local distance = (position - tool.Handle.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
local rayMesh = Instance.new("CylinderMesh", rayPart)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("Bright red")
rayPart.Transparency = 0.3
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.5, distance, 0.5)
rayPart.CFrame = CFrame.new(position, tool.Handle.CFrame.p) * CFrame.new(0, -distance/2, 0)
end)
end)

Report Abuse
TheDoctor4816 is not online. TheDoctor4816
Joined: 08 Feb 2009
Total Posts: 396
11 Nov 2013 03:37 PM
Ok, I think I've sorted it!
Report Abuse
XAXA is not online. XAXA
Joined: 10 Aug 2008
Total Posts: 6315
11 Nov 2013 03:39 PM
local ray = Ray.new(tool.Handle.CFrame.p, (tool.Handle.CFrame * CFrame.Angles(math.rad(-90), 0, 0)).lookVector*300)
Report Abuse
TheDoctor4816 is not online. TheDoctor4816
Joined: 08 Feb 2009
Total Posts: 396
11 Nov 2013 03:51 PM
Thanks for all your help!
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