New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
Log in to vote

How can i lock my mouse in the center, but have my camera type on scriptable?

Asked by
RubenKan 3570 Moderation Voter Administrator Community Moderator
3 years ago
Edited 3 years ago

My problem is that i have to use Camera:SetRoll(Angle), which is only available to cameras with their CameraType set to Scriptable.

However, UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter doesn't work when the camera is on Scriptable.

Is there any other way to lock the mouse in the center, while being able to read out MouseDelta? I can't make the character fly around in first person, because that also doesn't allow me to use MouseBehavior.

TL;DR; I need a way to lock the mouse in the center of the screen while the CurrentCamera type is Scriptable, and still being able to read out InputObject.MouseDelta.

I would put the camera and mouse in one script. Maybe use the "while true do" loop. See if that helps. :) Admin8483 -21 — 3y

2 answers

Log in to vote
Answered by 3 years ago

If you name a local script 'CameraScript' and place it inside StarterPlayerScripts, it will replace the default CameraScript with your script.

When using a normal local script inside somewhere that isn't StarterPlayerScripts this won't work, but if you use this method you can.

For example I can name my local script 'CameraScript' and place it in StarterPlayerScripts. I can then add this:

local userInputService = game:GetService('UserInputService')

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

wait() -- You can remove this, but I do this just in case.

camera.CameraType = Enum.CameraType.Scriptable

userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

userInputService.InputChanged:connect(function(inputObject) -- You can still read mouse delta.
    if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
        print('Delta X:' .. tostring(inputObject.Delta.x))
        print('Delta Y:' .. tostring(inputObject.Delta.x))

You can read more about this on the wiki page here.

Thank you! This completely solves the problem. Now it's time for math ;-; RubenKan 3570 — 3y
Log in to vote
Answered by
Admin8483 -21
3 years ago

Wait... just never move the mouse!

Wow he totally never thought of that... cough....cough....dumb. farrizbb 404 — 3y
Well guess what? The game resolves around moving your mouse to rotate the map around.. Suggest to remove this answer before you get too many downvotes ;p RubenKan 3570 — 3y

Answer this question