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!




local tool = script.Parent local deb = game:GetService("Debris") script.Parent:WaitForChild('RemoteEvent').OnServerEvent:Connect(function(player,mouse,hit) print("Making ray") local ray = Ray.new(tool.CFrame.p, (hit - tool.CFrame.p).unit * 3000) local part, position = workspace:FindPartOnRay(ray, tool, false, true) local beam = Instance.new("Part", workspace) beam.BrickColor = BrickColor.Red() beam.FormFactor = "Custom" beam.Material = "Neon" beam.Transparency = 0.25 beam.Anchored = true beam.Locked = true beam.CanCollide = false deb:AddItem(beam,1) beam.Touched:Connect(function(hit) print("Hit ".. hit.Name) end) local distance = (tool.CFrame.p - position).magnitude beam.Size = Vector3.new(0.3, 0.3, distance) beam.CFrame = CFrame.new(tool.CFrame.p, position) * CFrame.new(0, 0, -distance / 2) print("Made") end)

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

local tool = script.Parent

local deb = game:GetService("Debris")


script.Parent:WaitForChild('RemoteEvent').OnServerEvent:Connect(function(player,mouse,hit)
    print("Making ray")
    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
    local part, position = workspace:FindPartOnRay(ray, tool, false, true)

    local beam = Instance.new("Part", workspace)
    beam.BrickColor = BrickColor.Red()
    beam.FormFactor = "Custom"
    beam.Material = "Neon"
    beam.Transparency = 0.25
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false
    deb:AddItem(beam,1)
    beam.Touched:Connect(function(hit)
        print("Hit ".. hit.Name)
    end)
    local distance = (tool.CFrame.p - position).magnitude
    beam.Size = Vector3.new(0.3, 0.3, distance)
    beam.CFrame = CFrame.new(tool.CFrame.p, position) * CFrame.new(0, 0, -distance/2)
    print("Made")
end)
Ad

Answer this question