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

Having trouble with raycasting and CFrames?

Asked by 4 years ago

Hello, I'm experimenting with rays and i want the end of a part to point at my mouse but I'm having trouble, When I run this script it makes a part, but it goes in a random direction, Can anybody help? Thanks!

01local tool = script.Parent
02 
03local deb = game:GetService("Debris")
04 
05 
06script.Parent:WaitForChild('RemoteEvent').OnServerEvent:Connect(function(player,mouse,hit)
07    print("Making ray")
08    local ray = Ray.new(tool.CFrame.p, (hit - tool.CFrame.p).unit * 3000)
09    local part, position = workspace:FindPartOnRay(ray, tool, false, true)
10 
11    local beam = Instance.new("Part", workspace)
12    beam.BrickColor = BrickColor.Red()
13    beam.FormFactor = "Custom"
14    beam.Material = "Neon"
15    beam.Transparency = 0.25
View all 27 lines...

I know nothing about rays so i tried googling it but had no luck, Thanks for any help!

1 answer

Log in to vote
1
Answered by 4 years ago

I tested this and it should work, I don't really see any problems with this unless either you didn't send mouse.Hit.p through your remote event or your tool isn't on your character

01local tool = script.Parent
02 
03local deb = game:GetService("Debris")
04 
05 
06script.Parent:WaitForChild('RemoteEvent').OnServerEvent:Connect(function(player,mouse,hit)
07    print("Making ray")
08    local ray = Ray.new(tool.CFrame.p, hit.p - tool.CFrame.p) -- you didn't need to use .unit so I made it cleaner, also if you weren't sending hit.p through your RemoteEvent that might have been the problem otherwise delete the .p I put
09    local part, position = workspace:FindPartOnRay(ray, tool, false, true)
10 
11    local beam = Instance.new("Part", workspace)
12    beam.BrickColor = BrickColor.Red()
13    beam.FormFactor = "Custom"
14    beam.Material = "Neon"
15    beam.Transparency = 0.25
View all 27 lines...
Ad

Answer this question