|
| 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 |
|
|
|
| 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 |
|
|
| |
|
|
| 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 |
|
|
|
| 11 Nov 2013 03:22 PM |
| You're not setting its first position. |
|
|
| Report Abuse |
|
|
| |
|
|
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 11 Nov 2013 03:30 PM |
| unit is not a valid member..... |
|
|
| Report Abuse |
|
|
|
| 11 Nov 2013 03:30 PM |
| Sorry, didn't see the correction! |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 11 Nov 2013 03:37 PM |
| Ok, I think I've sorted it! |
|
|
| Report Abuse |
|
|
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 |
|
|
| |
|