Im trying to get a Vector3 from the ViewportPointToRay() function.
Here’s my function for handiling this so far.
function createRay(origin, dir, isCamera)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
if isCamera then
local camera = workspace.CurrentCamera
local viewportPoint = camera.ViewportSize / 2
local unitRay = camera:ViewportPointToRay(viewportPoint.X, viewportPoint.Y, 10)
print(unitRay.Position)
--local part = Instance.new("Part")
--part.Parent = workspace
--part.Position = unitRay
return unitRay
end
if not isCamera then
local rayOrigin = origin
local rayDir = dir
local raycastResult = workspace:Raycast(rayOrigin, rayDir)
end
end
What do you mean? What is the Vector3 supposed to represent? The ray’s origin? Direction? Where it would intersect terrain if cast? You need to provide more information.
where the ray ends or where it would intersect with a hit?
Well, the ray.Direction.Magnitude value should tell you how long the ray itself is. Depending on what you wanted to use the end of the ray value for, you can just use that instead.
What’s your use case for this? There might be an easier way to do it.
@yousef_mash1 is right, you can check the RaycastResult of a cast to see where it ends.
--this example excludes the local player's character
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local params = RaycastParams.new()
params.FilterDescendantsInstances = {char}
params.FilterType = Enum.RaycastFilterType.Exclude
--convert
local val = workspace.CurrentCamera.ViewportSize / 2
local ray = workspace.CurrentCamera:ViewportPointToRay(val.X, val.Y, 10)
--cast
local hit = workspace:Raycast(ray.Origin, ray.Direction, params)
print(hit and "Ray hit position: "..hit.Position or "Ray did not intersect anything")
local camera = workspace.CurrentCamera
local viewportPoint = camera.ViewportSize / 2
local unitRay = camera:ViewportPointToRay(viewportPoint.X, viewportPoint.Y, 10)
local hit = workspace:Raycast(unitRay.Origin, unitRay.Direction, raycastParams)
print(hit)
--local part = Instance.new("Part")
--part.Parent = workspace
--part.Position = unitRay
return unitRay