Mouse.Hit vs Mouse.UnitRay

Im making a placement system and i am trying to decide which is better to determine the position of the object im placing. Mouse.Hit or Mouse.UnitRay?

2 Likes

I only used Mouse.Hit and in this case I would use Mouse.Hit. I think that getting a CFrame of where player’s mouse is pointing in 3D space is better than raycasting from player’s camera to mouse location. I think that reading Mouse.UnitRay API might be useful: Mouse.UnitRay (roblox.com).

1 Like

Mouse.Hit.p may also be of use to you. If you prefer working with Vector3 values over CFrame values that is.

2 Likes

Mouse.Hit.p is deprecated, you should use Mouse.Hit.Position

1 Like

Thanks for bringing new information to this thread

you should not use Mouse anymore

here is how to do it using UserInputService

local userInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
local raycastParams = RaycastParams.new()

local function GetMouseHit()
    local mouse = userInputService:GetMouseLocation()
    local ray = camera:ViewportPointToRay(mouse.X, mouse.Y)
    local raycastResult = workspace:Raycast(ray.Origin, ray.Direction * 1000, raycastParams)
    if raycastResult == nil then
        return ray.Origen + ray.Direction * 1000
    else
        return raycastResult.Position, raycastResult.Instance
    end
end

while true do
    local hit, target = GetMouseHit()
    print(hit, target)
    task.wait(1)
end

and now you get all the benefits of using RaycastParams and RaycastResult

4 Likes

yes, you are right.
This is actually the best way to get mouse hit currently