I am working on a simple first person “shooter” game where unlocking and relocking the mouse position is essential during gameplay. My problem is that whenever I re-lock the mouse to the center of the screen, the entire camera jerks in the direction of the mouse relative to the center of the screen.
To achieve the first-person effect I am using Enum.CameraMode.LockFirstPerson, and I am unlocking the mouse by toggling the .Modal property on an invisible UI button covering the screen. This seems like a janky solution, but I haven’t had much luck using UserInputService.MouseBehavior to unlock the mouse.
I don’t expect to be able to achieve smooth motion with this mechanic, but is it possible to reset the mouse position before locking it back to the camera to prevent this jerky movement?
I have attached a short video (forgive the quality) to demonstrate my issue.
So, I have had this work perfectly fine using UserInputService.MouseBehavior before.
What exactly is your issue using that? If it is the same, I would suggest using Enum.MouseBehavior.LockCurrentPosition instead of Enum.MouseBehavior.LockCenter
Using Enum.MouseBehavior does not lock or unlock the mouse for me. I’ve only been able to get it to work with .Modal on a gui button. I’m guessing its because I’m locking first person with Enum.CameraMode.LockFirstPerson, but I’m really not sure how I would do it otherwise if that was the problem.
I set the CameraMode back to classic and set the CameraMaxZoomDistance to 0.5 to achieve the same first-person effect, but changing the MouseBehavior still does not unlock the mouse.
so it looks like LockFirstPerson sets the MouseBehavior every time the camera is updated in the render step, which is a little annoying. This is how you can do this:
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
RunService:BindToRenderStep("UnlockFirstPersonMouse", Enum.RenderPriority.Camera.Value + 100, function()
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end)
-- to stop the unlock:
-- RunService:UnbindFromRenderStep("UnlockFirstPersonMouse")
I will explain the code if you can verify that it works, and want an explanation
The same code I sent does appear to be working fine for me when I do this. I might have to see more of your code to tell you why it is doing that (unless you are talking about the jerk you see halfway through my video, that is unavoidable without creating a camera controller)
I think I found a solution. I was under the impression that mouse sensitivity was not writable, but I was completely wrong. It looks like setting mouse sensitivity to 0 and then tweening it back to its normal removes the jerk and makes the movement smoother. Thank you for your help, moving away from the Modal trick makes this a lot neater.
I also think that part of the problem was partly due to the device emulation I was using in Studio to test UI. Disabling it and testing again gave better results even after the previous fixes.