Tracking PlayerMouse.Hit while CurrentCamera is set to Scriptable

Hi there! I’m having a strange problem where PlayerMouse.Hit is significantly off from the actual position of the cursor’s position in the 3D world while the CurrentCamera is set to Scriptable.

https://i.gyazo.com/f37a8de84b6aa6f907e435e3c91c6596.mp4

In the video above, the small, white brick shows the PlayerMouse.Hit position which is visibly off by a significant factor in comparison to the actual mouse. Also testing the Target property, I found out that the result value agrees with the Hit.

There are times (maybe like a 10% chance) where the player mouse actually does work as intended. What makes me confident that this is definitely a Roblox issue to some extent is that the mouse behaves as expected when the camera settings are not set to Scriptable (refer to video below).

https://i.gyazo.com/b8f7adb5acf4f52111f27634e75f78e7.mp4

Here are some code snippets, but I’m not sure if they give much detail.

mouse.Button1Down:connect(function()
    mouseDown = true
    while mouseDown do
        game:GetService("RunService").RenderStepped:wait()
        local target = mouse.Hit.p + Vector3.new(0, currentPos.y - mouse.Hit.p.y, 0)
        currentPos = (CFrame.new(currentPos, target) * CFrame.new(0, 0, -0.25)).p
    end
end)

spawn(function()
	while true do
		cursorPart.CFrame = mouse.Hit
		local target = mouse.Hit.p + Vector3.new(0, currentPos.y - mouse.Hit.p.y, 0)
		local newCFrame = CFrame.new(currentPos, target)
		pet:SetPrimaryPartCFrame(newCFrame)
		
        -- If the line below is commented (as well as the camera.CameraType = "Scriptable" which is not listed), it works fine)
		camera.CFrame = CFrame.new(pet.PrimaryPart.CFrame.p) * CFrame.Angles(math.rad(-70), math.rad(0), math.rad(0)) * CFrame.new(0, 0, 20) 
			
		game:GetService("RunService").RenderStepped:wait()
	end
end)
1 Like

Kinda gravedigging, but I tried using the code you gave while also setting the camera type to Scriptable and trying it out in different orientations (even up side down) and it worked perfectly fine.

wait()
local mouse = game.Players.LocalPlayer:GetMouse()
local currentPos = Vector3.new()
local posPart = Instance.new("Part",workspace)
local mouseDown=true
mouse.Button1Down:connect(function()
    mouseDown = true
    while mouseDown do
        game:GetService("RunService").RenderStepped:wait()
        local target = mouse.Hit.p + Vector3.new(0, currentPos.y - mouse.Hit.p.y, 0)
        currentPos = (CFrame.new(currentPos, target) * CFrame.new(0, 0, -0.25)).p
		posPart.CFrame=CFrame.new(currentPos)
    end
end)
mouse.Button1Up:Connect(function()
	mouseDown=false
end)

This may be giving you inconsistent or undesired execution order of your code. I’d recommend using BindToRenderStep instead.

2 Likes

Works perfectly fine for me though. Also, having to bind and unbind the function on every click seems a bit tedious.
That is considering you weren’t only talking about the second part.

Anyways, this is what I have right now and it works perfectly, even though the part seems to “sit down” when not in motion.

local camera = workspace.CurrentCamera
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

local currentPos = Vector3.new()

local posPart = Instance.new("Part",workspace)
local rposPart = Instance.new("Part",workspace)
local rposPart2 = Instance.new("Part",workspace)

local mouseDown = false
camera.CameraType=Enum.CameraType.Scriptable
mouse.TargetFilter = rposPart

mouse.Button1Down:connect(function()
    mouseDown = true
    while mouseDown do
        game:GetService("RunService").RenderStepped:wait()
        local target = mouse.Hit.p + Vector3.new(0, currentPos.y - mouse.Hit.p.y, 0)
        currentPos = (CFrame.new(currentPos, target) * CFrame.new(0, 0, -0.25)).p
        posPart.CFrame=CFrame.new(currentPos)
    end
end)
mouse.Button1Up:Connect(function()
    mouseDown=false
end)
local lt = tick()
game:GetService("RunService").RenderStepped:Connect(function()
    local t=tick() local ti=t-lt
    rposPart.CFrame=mouse.Hit
    local target = mouse.Hit.p + Vector3.new(0, currentPos.y - mouse.Hit.p.y, 0)
    local newCFrame = CFrame.new(currentPos, target)
    rposPart2.CFrame=newCFrame
    workspace.Camera.CFrame = CFrame.new(posPart.CFrame.p) * CFrame.Angles(math.rad(-70), math.rad(0), math.rad(0)) * CFrame.new(0, 0, 20)
end)