I want to cast a ray inside of a ViewportFrame towards the mouse’s position but it doesn’t seem to be as simple as it sounds. Here’s the code that I’m using:
function CanvasModule:CreatePixel()
local MousePos = UserInputService:GetMouseLocation() -- Mouse 2d space
local DrawUnitRay = self.CanvasCamera:ViewportPointToRay(MousePos.X, MousePos.Y - 36) -- UnitRay
local DrawRayCast = self.WorldModel:Raycast(self.CanvasCamera.CFrame.Position, DrawUnitRay.Direction * 100) -- Casting the ray towards the mouse position
if DrawRayCast then
print("Raycast hit something")
else
print("Raycast didn't hit anything")
end
end
Note that there is no empty space in the cameras view and instead there’s a brick in front of it so if the ray was casting in the right direction there would be no possible way that the ray wouldn’t hit anything.
The ray is shooting in a completely different direction than what would be expected.
CurrentCamera’s lookvector: -0, -0, -1
DrawUnitRay.Direction: 0.90532231330872, -0.42472475767136, -0.00064873765222728
The method I’m using must not work with ViewportFrames but I can’t think of another way to convert the mouse’s 2D position into 3D space.