Hello!
I’m trying to create a ray cast from one brick to another brick, and tried reading the article about it on Roblox Wiki. However, I am confused on the math operations involving the direction of the ray cast.
Here’s the code from Roblox Wiki:
local ray = Ray.new(
Weapon.Handle.CFrame.p, -- origin
(mouse.Hit.p - Weapon.Handle.CFrame.p).unit * 500 -- direction
)
local ignore = game.Players.LocalPlayer.Character
local hit, position, normal = Workspace:FindPartOnRay(ray, ignore)
The green line represents the direction from point 0 to point 1… The unit vector represents the SAME direction, it’s just that it’s a little more simplified (which makes it useful in different scenarios)
I tried creating the beam you were going on about, but the beam doesn’t seem to display for me. Would you mind helping me find out the errors or mistakes I made. Here’s the code I created:
local startPart = script.Parent.start
local finishPart = script.Parent.finish
function Raycasting()
local ray = Ray.new(startPart.Position, finishPart.Position)
local part, position = workspace:FindPartOnRay(ray, startPart)
local startAtt = Instance.new("Attachment", startPart)
startAtt.Position = startPart.Position
local finishAtt = Instance.new("Attachment", finishPart)
finishAtt.Position = finishPart.Position
local beam = Instance.new("Beam")
beam.Attachment0 = startAtt
beam.Attachment1 = finishAtt
beam.Parent = startAtt
beam.Width0 = 1
beam.Width1 = 1
beam.Enabled = true
end
Raycasting()
Also, I noticed people used a part instance instead of the beam instance, How would I go on doing that?
I believe this is the issue, you’re parenting the beam to an attachment which is already a child of the beam.
Setting the parent of the beam to Terrain is usually what I do.