So I want to get the mouse location in the world space without using mouse.hit.p. Im wondering how I do this. Thanks.
Mouse.UnitRay is always defined, which would mean that you can safely use Mouse.UnitRay.Direction.
Why can’t you use mouse.Hit.p?
can you show a code sample? Im trying to figure this out
Thanks, im trying to not use mouse, incase it gets deprecated in the future. Is there any userinputseevice alternatives?
Just realized it was on the devhub. My bad lol
https://developer.roblox.com/en-us/api-reference/function/Camera/ScreenPointToRay
deprecation does not usually mean it will stop working, it’s just bad practice
plus, mouse is used by a lot of games so removal without a really major reason wouldn’t be a good play from roblox
local Enumeration = Enum
local Game = game
local Workspace = workspace
local UserInput = Game:GetService("UserInputService")
local Camera = Workspace.CurrentCamera
local function OnInputBegan(InputObject, GameProcessed)
if GameProcessed then return end
if InputObject.UserInputType ~= Enumeration.UserInputType.MouseButton1 then return end
local Ray = Camera:ViewportPointToRay(InputObject.Position.X, InputObject.Position.Y, 0)
local Result = Workspace:Raycast(Ray.Origin, Ray.Direction * 250)
if not Result then return end
print(Result.Instance)
end
UserInput.InputBegan:Connect(OnInputBegan)
I dont want to use raycasting because if the person is pointing at the sky, it wont get the pos.
nvm im dumb. that doesnt get mouse pos.
The solution posted by Forummer is correct, and it is the Roblox recommended way of getting the mouse location. Roblox internally is just using Raycasting, just with more protection. If you don’t want to deal with errors or nil value when a player points at the sky, then just have a guard clause before calculating to assure the raycast actually hit a part. Roblox is actively sunsetting the Mouse
instance, as they want everyone to use the new APIs.
You can still get the position by adding the direction to the origin (if the ray did not interact with anything).