Is it possible to get the endpoint of a ray if it doesn't intersect with anything?

Greetings,

The title pretty much sums it up. I want to get the endpoint of a ray that went 10 studs in a certain direction and turn that endpoint into a vector3

local rayOrigin = player.Character.HumanoidRootPart.Position
local rayDirection = (mousePosition - rayOrigin).Unit * 10

– Build a “RaycastParams” object and cast the ray
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

local beam = Instance.new(“Beam”)
beam.Attachment0 = player.Character.HumanoidRootPart.RootRigAttachment
beam.Parent = player.Character.HumanoidRootPart

local Attachment = Instance.new(“Attachment”)

if raycastResult then
print (raycastResult)
Attachment.Position = raycastResult.Position
Attachment.Parent = workspace.Terrain
beam.Attachment1 = Attachment

else
print “no”
Attachment.Position = raycastResult.Position
Attachment.Parent = workspace.Terrain
beam.Attachment1 = Attachment
end

I connect a beam to the end point just to visualize the result however if it doesn’t intersect with anything raycastResult.Position returns nil

Thank you for your time

Logically, if the ray didn’t hit anything, the end position would be rayOrigin + rayDirection.

3 Likes

brilliant, thank you very much