Need help with VeiwportPointToRay

Hello, I have been messing around with the ViewportPointToRay method as part of an alternative solution to mouse.hit. However, as you can see from the video below, there is an offset to direction in relation to the mouse cursor. Any help would be appreciated:

https://gyazo.com/80d9f2a44b1f7644adfa1387cdc454b0

Code snippet:

local function throwSnowball(userInput) -- Userinput object from InputBegan
	-- First we need to calculate the direction the player hit:
	local inputPosition = userInput.Position
	local positionRay = CurrentCamera:ViewportPointToRay(inputPosition.x, inputPosition.y)
	local rayDirection = positionRay.Direction
	
	local p = Instance.new("Part",workspace)
	p.Position = rayDirection * 100
	
	--SnowballRemote:FireServer(rayDirection)
end

Your script is not respecting positionRay.Origin.
Try p.Position = positionRay.Origin + rayDirection * 100

@TheCarbyneUniverse
OP wants to transform the screen position to a world position, which is done using the ray.

2 Likes