I don’t know how to account for the Gui Inset when casting a ray at the mouse, mouse.Hit and using ScreenPointToRay both don’t account for the Inset and the wiki says they recommend ScreenPointToRay as it’s the most accurate but still doesn’t account for Inset.
local mouse = client.player:GetMouse()
local pos = client.UIS:GetMouseLocation()
local unitRay = workspace.CurrentCamera:ScreenPointToRay(pos.X, pos.Y)
local result = client.gen.RC(unitRay.Origin, unitRay.Direction * 1000)
if result then pos = result.Position else pos = unitRay.Direction * 1000 end
I did this and now it’s off point sometimes but on point others?
local mouse = client.player:GetMouse()
local pos = client.UIS:GetMouseLocation()
local inset = game:GetService("GuiService"):GetGuiInset()
local unitRay = workspace.CurrentCamera:ScreenPointToRay(pos.X + inset.Y, pos.Y - inset.Y)
local result = client.gen.RC(unitRay.Origin, unitRay.Direction * 1000)
You’re mixing your axes there.
Also whenever you click on the skybox or miss what you are trying to aim at, your actual aim could be off due to the difference between the camera position and the character position.
The player mouse is quite outdated and the Roblox developer API recommends the use of the UserInputService instead. UIS provides automatic inset calculation, which I believe suits your needs.
I know it’s generally frowned upon to spoon-feed code, but I believe you know enough LUA to understand the code just by reading it.
I’ve coded about 30 lines that achieves what I believe you were looking for:
local UIS = game:GetService("UserInputService")
local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
--// Raycast Parameters //--
local params = RaycastParams.new()
params.FilterDescendantsInstances = {char}
params.FilterType = Enum.RaycastFilterType.Blacklist
--// Mouse Position 3D: 2D Screen -> 3D Space //--
local function mPos3D(pos)
local ray = cam:ScreenPointToRay(pos.X, pos.Y)
local result = workspace:Raycast(ray.Origin, ray.Direction * 100, params)
if result then return result end
end
--// Mouse Button 1 Down (LMB) //--
UIS.InputBegan:Connect(function(input, gp)
if gp then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local result = mPos3D(input.Position)
if result then
workspace.Part.Position = result.Position
end
end
end)
Here is an example video of the code above:
If you have any further questions feel free to ask.
Hope this helps!