How does Raycast work?

The title speaks for itself

How does Raycast work?

Also, How can I make a active beam showing the Raycast so its not invisible

1 Like

In simple words is an invisible beam that is shot from an origin to a point, you can create distances and variables for it (Everything that has to do with positions is used Vector3)
Example: “Block”:Raycast(rayOrigin, rayDirection)
I recommend you see this article.

To make an active beam, create an “Attachment” for each block, origin and target.
then insert a “Beam” into the part.
If you want the part to shoot it, make movement with CFrame and put a distance to it, hope I have helped you…

Thanks! I will try to test this out today!

Im a bit confused on how Im supposed to make it visible, the wording seems complicated

Here’s how you’d visualize a ray:

local ray = workspace:Raycast(workspace.Part.Position, workspace.GoalPart.Position - workspace.Part.Position) 
local p = Instance.new('Part') 
p.Parent = workspace 
p.Position = (workspace.Part.Position + ray.Position) / 2 
p.Size = Vector3.new(0.1,0.1,(workspace.Part.Position - ray.Position).Magnitude) 
p.CFrame = CFrame.lookAt(p.Position, workspace.Part.Position)
p.Material = Enum.Material.Neon

I wrote this response which involves a more visual approach to understanding raycasting if you want to have a read:

Can you explain the code? I don’t understand it especially because everything is so close to each other

This where you’re actually casting the ray:

local ray = workspace:Raycast(workspace.Part.Position, workspace.GoalPart.Position - workspace.Part.Position)
-- this is to create a new part:
local p = Instance.new('Part') 
p.Parent = workspace 
-- this takes the two parts' positions, adds them, then divides it by 2 which finds the average position which is also the middle of the 2 parts:
p.Position = (workspace.Part.Position + ray.Position) / 2 
-- this finds the distance between the ray's hit position and the origin, then makes the part as long as the distance between the 2 points:
p.Size = Vector3.new(0.1,0.1,(workspace.Part.Position - ray.Position).Magnitude) 
-- this makes the part look at the "goal" part:
p.CFrame = CFrame.lookAt(p.Position, workspace.Part.Position)
p.Material = Enum.Material.Neon