How do you visualize a raycast as a part?

So I’m doing raycasting and understand it, but how do you make a part to visualize the raycast? Also please explain why each part is which so I get a better understanding.

16 Likes
4 Likes

Here is a developer hub article on basic raycasting, that might help you out!

https://developer.roblox.com/en-us/articles/Raycasting

2 Likes

This is basically perfect but can you explain this part (especially the -distance/2):

p.CFrame = lookAt(position, origin)*CFrame.new(0, 0, -distance/2) 
1 Like

It is explained there.

Basically, without that offset, the part would be in the middle. You want its back face to be at the origin.

The part would be positioned right where the origin is.

Now, that post was using the old raycasting API. I suggest you use the new one now.

-- for more info on these, check out:
-- https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast
-- https://developer.roblox.com/en-us/api-reference/datatype/RaycastParams
local result = workspace:Raycast(origin, direction, params)

if result then
    local distance = (origin - result.Position).Magnitude
    local p = Instance.new("Part")
    p.Anchored = true
    p.CanCollide = false
    p.Size = Vector3.new(0.1, 0.1, distance)
    p.CFrame = CFrame.lookAt(origin, position)*CFrame.new(0, 0, -distance/2)
end

I will update the post I linked to use the new one as well.

70 Likes

OH wait I get it now.

It gets positioned to the middle (like everything that gets positioned via script) which means you only need half of the part to be offset for it to be completely in front (so divide its length by 2), and then the negative moves it forwards instead of backwards like positive integers would on the Z axis. Please correct me if I’m wrong

And then I’m good with CFrames so I understand the lookAt part the only thing I didnt understand was the -distance/2, it looked way more complicated than it actually was

Alright, thanks! And I’ve already learned the new Raycasting API :+1:

2 Likes

Correct! I was going to show another example for if there was still some confusion. You indeed only need “half of the part”!

2 Likes

Where are you getting position from?

7 Likes

If you mean RaycastResult.Position then it is a property of raycastresult which is basically is a vector value which shows where the ray got intersected.

1 Like

Something i did was create an anchored an dinvisible part , and then created two attachement, one inside the new part and the other in the base of the raycast. Every now and then the part.position = raycastResult.Position

I’ve came back to this post because I think this code is really useful for debugging. However, it only seems to work if the ray actually hits something, since part of the code relies on the ray’s hit position to spawn the part. Here is another alternative I wrote myself which visualizes a ray regardless of if it hit anything:

local function visualizeRaycast(origin, direction)
	local Raycast = Instance.new("Part")
	Raycast.CFrame = CFrame.new(origin + direction/2, origin + direction)
	Raycast.Anchored = true
	Raycast.CanCollide = false
	Raycast.Name = "Raycast"
	Raycast.Size = Vector3.new(1, 1, direction.Magnitude)
	Raycast.Parent = workspace
end
6 Likes