mouse.Hit inaccuracy with camera manipulation

I’m currently working on a click to move script which uses mouse.hit.p. Along with this, I have some basic camera manipulation which allows the camera to be stationary. mouse.hit.p only seems to be inaccurate and off when the camera is stationary, as shown in the GIF.
https://gyazo.com/a920a4dc7417d882c1c60d42c24f045f

Here is my click to move script, found in StarterGui


local plr = game.Players.LocalPlayer
local humanoid = plr.Character:WaitForChild("Humanoid")

UserInputService.InputBegan:Connect(function(input)
   local inputType = input.UserInputType
   if inputType == Enum.UserInputType.MouseButton1 then
        
        local mouse = plr:GetMouse()
        humanoid:MoveTo(mouse.Hit.p)
    end
end)
1 Like

That is weird, I put your exact code in studio the exact way you did, so maybe it’s another script affecting your players movement.

Mouse.Hit simply fires a raycast from the camera at a direction calculated based on the Mouse’s X and Y positions and the screen’s size. Simply replicate Mouse.Hit’s algorithm yourself, as I am quite sure Roblox has done things to optimize it.

Yeah. Whenever I remove the camera manipulation script, it works as intended.

1 Like

Could you go a little more in depth on how I’d approach this?

My solution to this also gives an accurate position. It is kind of all over the place.

local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera

function RayCast(Position, Direction, MaxDistance, IgnoreList)
	return game:GetService("Workspace"):FindPartOnRayWithIgnoreList(Ray.new(Position, Direction.unit * (MaxDistance or 999)), IgnoreList)
end

local function castBeam()
	local MaxDistance = 60
	local IgnoreList = {Player.Character}
	local UnitRay = Camera:ScreenPointToRay(mouse.X, mouse.Y)
	local Origin = UnitRay.Origin
	local Direction = UnitRay.Direction
	local instance, HitPoint = RayCast(Origin, Direction, MaxDistance, IgnoreList) -- Will goto max distance unless something is hit,
	--[[
	HitPoint returns the vector3 position
	instance returns the Object that was hit or nil if nothing was hit
	]]
	print(instance, HitPoint)
end
1 Like
function GetDegreesFromMouse()
    local ScreenX = Mouse.ScreenSizeX
    local ScreenY = Mouse.ScreenSizeY

    local CorrectedMouseX = (Mouse.X - ScreenX)/ScreenX--get mouse location on scale -1 to 1
    local CorrectedMouseY = (Mouse.Y - ScreenY)/ScreenY

    local HorizontalFOV = workspace.Camera.FieldOfView
    local VerticalFOV = 2*math.atan(math.tan(HorizontalFOV/2)*(16/9))--assume aspect ratio is 16:9

    local HorizontalDegrees = HorizontalFOV*CorrectedMouseX
    local VerticalDegrees = VerticalFOV*CorrectedMouseY

    return (HorizontalDegrees,VerticalDegrees)
end

This will return the degrees in which you must “rotate” the direction vector to fire in. Using some basic trigonometry you can convert these degrees into a Unit Vector3.

If you intend on having support for other aspect ratios, it’s best you change the (16/9) part to something more variable.