Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to cast a ray that points towards the mouse location and originates from the camera position?

Asked by 4 years ago

I need this done using user input service, the wiki claims that it is more powerful but lacks built in mouse functions. for anyone confused I basically need mouse.Hit but using user input service. I have tried using camera:ViewPortPointToRay(X, Y, 1) but that doesn't seem to make a ray that points towards the mouses location in 3d space but instead the look vector of the cameras cframe. I want the mouses position in 3d space, not the cameras look vector. passing the mouses x and y into viewport doesn't seem to make a ray pointing to the vector 3 equivilent.

UIS.InputBegan:Connect(function(input,processed)
    if processed then return end
    if input.KeyCode == Enum.KeyCode.E then
        local mouseLocation = UIS:GetMouseLocation()
        local unitRay = camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y * 0)
        print("Camera positioin: " .. tostring(camera.CFrame.Position))
        print("Unit ray position: " .. tostring(unitRay.Direction))
        local p = Instance.new("Part", workspace)
        p.Size = Vector3.new(1, 1, 1)
        p.Anchored = true
        p.BrickColor = BrickColor.Blue()
        p.CFrame = CFrame.new(unitRay.Origin, unitRay.Direction)
    end
end)

Answer this question