I made a handy resource for Roblox developers to support “Mouse Lock” (aka “Shift Lock”) mode in their games for mobile players!
Also consider getting the Model from the Roblox website so it’s available in Studio’s toolbox and on your Roblox account!
What is mouse lock (shift lock)?
This handy feature faces your character in the direction your camera is facing, and is highly useful in with melee combat and role-play games. Roblox players who have used the mobile app have probably noticed that the “mouse lock” or “shift lock” doesn’t exist on touch-enabled devices by default. It gives the player far better control over the direction their character is facing than the keyboard or on-screen joystick.
Unfortunately, this great feature is unavailable by default on mobile Roblox. I may not be able to change that, but I can provide fellow Roblox developers with this nice little package which enables them to add it to their games and experiences!
Want your favorite Roblox game to support mobile shift lock? Send them this post! I’ve made this pack super straightforward to install.
What’s in the box?
- A replacement PlayerModuleLoader LocalScript for placing in StarterPlayerScripts. This overrides the one Roblox inserts by default with one that does some fiddling before it loads the entire PlayerModule.
- An imitation TopBar ScreenGui containing a button that toggles the lock mode. If you don’t want it, you can make your own button using the code in it! It’s just an example that I built to look nice with Roblox’s default UI.
- An “INSTRUCTIONS” ModuleScript for those who need the nitty-gritty.
How to enable shift lock
- First, insert the object into your place
- After getting the model from the Roblox website, open the Toolbox and insert it, OR
- Drag the downloaded model file in your filesystem onto Roblox Studio
- Move the PlayerModuleLoader LocalScript into StarterPlayer → StarterPlayerScripts.
- Put the MouseLockToggle ScreenGui inside StarterGui.
- Stash the original folder with the INSTRUCTIONS in ServerStorage you can find it again, just in case.
- That’s it! Test it out by turning on Studio’s device emulator, select a touch device and press play. You should be able to click the button to toggle mouse lock like in the video.
This was made possible by EgoMoose’s CameraModule API Public Override. It’s an interesting injection trick which fiddles with a part of the camera portion of the PlayerModule, which is normally locked down by the
UserRemoveTheCameraApi FFlag (see above). But, by using some really clever Lua environment injection via the
CameraModule is essentially tricked into thinking the flag isn’t set. Therefore, it exposes the object to the developer without needing to fork the entire PlayerModule system. Genius!
Moreover, I noticed that the constructor for the
CameraModule simply didn’t construct a
MouseLockController object when running on non-
TouchEnabled devices (see above). I added a small bit to the loader script that creates it manually if it got skipped over by the default code. The end result: it works exactly the same now.
Some Fine Print
- This will probably work for a while, though changes Roblox makes to the camera and mouse-lock parts of the PlayerModule may break or cause issues.
- I’m not liable if you break your game with this – no warranties included :–)
- Perhaps in the future Roblox will have formal support for mouse/shift lock for touch – it’s up to you to know whether or not that has happened yet. Search #updates:announcements on the DevForum just in case.
- I’m also probably not going to provide support or updates for this. But, you’ve got the wonderfully talented DevForum community is at your fingertips and can help you solve your problems.
Here’s that download button again in case you don’t want to scroll up all the way:
That’s all folks! Hopefully this helps Roblox developers add a well-loved feature to their games.