Hi. I don’t see a lot of Rays in code, but today I saw some code with Rays in it. So, I wanted to know if it’d be useful for development. So far, it’s a bit hard to understand.
-
What do you want to achieve? Knowing if Rays are useful or not.
-
What is the issue? Trying to understand Rays are a bit hard, and I have no idea if I’m wasting my time by trying to learn them.
-
What solutions have you thought of so far? Well, I originally tried making an Instance.new(“Part”, workspace) and putting the Part in a Ray position… but that didn’t exactly work out, as I didn’t even know where to start.
So yeah, this is basically all you need to know. An example of some code with Rays in it is in the Dev Hub. Take this for example:
local function createPart(position, processedByUI)
-- Do not create a part if the player clicked on a GUI/UI element
if processedByUI then
return
end
-- Get Vector3 world position from the Vector2 viewport position
local unitRay = camera:ViewportPointToRay(position.X, position.Y)
local ray = Ray.new(unitRay.Origin, unitRay.Direction * LENGTH)
local hitPart, worldPosition = workspace:FindPartOnRay(ray)
-- Create a new part at the world position if the player clicked on a part
-- Do not create a new part if player clicks on empty skybox
if hitPart then
local part = Instance.new("Part")
part.Parent = workspace
part.Anchored = true
part.Size = Vector3.new(1, 1, 1)
part.Position = worldPosition
end
end
When I actually typed some of it in, I noticed a term or two that was actually deprecated. Which, to me indicates that it either isn’t that important, or has not been updated yet. Is this true? If you can help, please do. (IF THIS TOPIC IS NOT IN THE RIGHT AREA, PLEASE LET ME KNOW WHERE TO PUT IT, SO I CAN CHANGE ITS LOCATION.)