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.
Here is a developer hub article on basic raycasting, that might help you out!
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)
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.
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
Correct! I was going to show another example for if there was still some confusion. You indeed only need “half of the part”!
Where are you getting position from?
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.