Get Vector3 from ViewportPointToRay

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.

I’m sorry, I kinda suck with raycast.

I’m trying to find where the raycast ends, and it’s Vector3 if that make sense.

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.

raycastResult.Position returns a vector3 of where the raycast hit.

note: make sure to check if raycastResult exists first

Probably this.

thirtycharslong

I’m focusing in the ViewPointToRay. Unless it exists.

@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")
1 Like

the only way to get the position of where ViewPointToRay ends is through the raycastResult.

		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

Why is it printing it nil? I’m kinda confused.

That’s because it didn’t hit anything. Try multiplying up the direction to get a larger value.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.