I want to forgo the mouse object and mouse.hit or mouse.target as the doccumentation says that it is better to use userinputservice. BUT I can not for the life of me figure out how to get a mouse position without using the mouse object. Is there something I am missing? Like an obscure Enum.UserInputType or something?
local mouse = game.UserInputService:GetMouseLocation()
local ray = workspace.Camera:ViewportPointToRay(mouse.X, mouse.Y)
local result = workspace:Raycast(ray.Origin, ray.Direction * 1000)
if result then
local hit = CFrame.new(result.Position)
end
If I may ask, Why do they want us to not use mouse.hit?
Because the Mouse object is deprecated or superseded by UserInputService.
It’s mostly about making your game have multi-platform support. UIS has much broader scope. Having said that, it’s not deprecated - but it might be in the future.
I just find that using the mouse object is cleaner and leads to easier to read code.
UIS.InputBegan:Connect(function(Input)
if Input.UserInputType==Enum.UserInputType.MouseButton1 then
local mouse = game.UserInputService:GetMouseLocation()
local ray = workspace.Camera:ViewportPointToRay(mouse.X, mouse.Y)
local result = workspace:Raycast(ray.Origin, ray.Direction * 1000)
if result then
local hit = result.Position--CFrame.new(result.Position)
makepart(hit)
end
end
end)
--compared to
UIS.InputBegan:Connect(function(Input)
if Input.UserInputType==Enum.UserInputType.MouseButton1 then
local hit=mouse.Hit.Position
makepart(hit)
end
end)
I am aware that in the second example, if you click on nothing it just makes a part in the middle of nowhere.
You could make a function so you don’t need to write it all out but I find the first example better because I don’t like the mixing of UserInputService and Mouse in the second one.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.