Help on Raycasting

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)
1 Like

The documentation for Vector3 should help with understanding how the direction of the raycast is calculated.

If you just want to create a ray between two parts, that would be quite simple.

local function CreateRayBetweenParts(part1,part2)
    return Ray.new(part1.Position,part2.Position)
end
1 Like

Thank you! And also, how would I make the ray cast visible?

Usually, most people end up going with a beam.

If you want to use a part to visualize the ray with a part, then you can place the part in the middle with the correct orientation and position.

2 Likes

(mouse.Hit.p - Weapon.Handle.CFrame.p).unit

This part of the math is referring to a unit vector…

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)

3 Likes

Doesnt This have to be a table?

I don’t know, I got this is off of roblox wiki

local ignore_list_table = {game.Players.LocalPlayer.Character, somePart, somethingElseIdk}
1 Like

FindPartOnRay takes an instance, FindPartOnRayWithIgnoreList takes a table

4 Likes

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.

I’ve tried changing the parent to terrain like this:

	beam.Attachment0 = workspace.Terrain
	beam.Attachment1 = workspace.Terrain

But it doesn’t seem to give me any results.

You’re changing the attachments which the Beam uses, not changing the parent. (Terrain isn’t an attachment)

beam.Parent = workspace.Terrain
1 Like

I created the beam, thanks to you, but the beam seems to be afar off. It looks like it can be connected, but is offsetted.

Can you show us what is happening? (a screenshot or something)

Here’s what it looks like:

Are you setting the attachments and positions correctly?

If you’re still using Terrain as the attachment, this might be the reason.

Also, you can create beams normally without scripts in studio.

If you parent the attachments to the parts then you don’t have to modify their positions.


image

2 Likes