Having trouble with raycasting and CFrames?
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!
01 | local tool = script.Parent |
03 | local deb = game:GetService( "Debris" ) |
06 | script.Parent:WaitForChild( 'RemoteEvent' ).OnServerEvent:Connect( function (player,mouse,hit) |
08 | local ray = Ray.new(tool.CFrame.p, (hit - tool.CFrame.p).unit * 3000 ) |
09 | local part, position = workspace:FindPartOnRay(ray, tool, false , true ) |
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 |
18 | beam.CanCollide = false |
20 | beam.Touched:Connect( function (hit) |
21 | print ( "Hit " .. hit.Name) |
23 | local distance = (tool.CFrame.p - position).magnitude |
24 | beam.Size = Vector 3. new( 0.3 , 0.3 , distance) |
25 | beam.CFrame = CFrame.new(tool.CFrame.p, position) * CFrame.new( 0 , 0 , -distance / 2 ) |
I know nothing about rays so i tried googling it but had no luck,
Thanks for any help!