This module will help you set up a smooth and customizable over-the-shoulder camera system, which is quite popular in video games. You don’t need to know how to script to set this up, however, it also allows considerable customization for those who do know how to script. You can grab the ROBLOX model from here or read the source on GitHub from here. You can also test the basic system out at this place.
Getting started is very easy. Just insert the module script wherever you prefer on the client and require it from a local script inside StarterPlayerScripts. Then you can just use the enable/disable functions as you will.
local OTS_CAMERA_SYSTEM = require(...) OTS_CAMERA_SYSTEM:Enable()
Before I talk about some of the advanced customization features. I have to introduce an important concept used in the system. The system uses different camera settings (for example when you press right-click to zoom in) and switches between them. You can define your own camera settings or change the existing ones. By default, there are two simple camera settings included:
Most of the settings are self-explanatory. The offset setting is the offset from the humanoid root part. The Sensitivity setting is multiplied with mouse delta and LerpSpeed (between 0 and 1) setting is used to lerp the final CFrame (except in the cases of obstruction where the transformation is instant) to give a catching-up effect.
You can switch between different camera settings using :SetActiveCameraSettings(cameraSettings) where cameraSettings is a case-sensitive string indicating which camera setting to switch to.
Through the use of :SetCharacterAlignment(aligned) function, you can decide whether or not you want the character to rotate with the camera. By default, the character isn’t aligned with the camera.
Another notable feature is provided by the :SetMouseStep(steppedIn) function. This refers to the status of the mouse. When the mouse is stepped in the camera system, it is being used to guide the angle movements. Once it is stepped out, it can be used to fulfill other needs, for example interacting with an inventory Gui. When the camera system is enabled, the mouse starts being stepped in.
You can also change the shoulder to look over from by using the :SetShoulderDirection(shoulderDirection) where shoulderDirection is either 1 (right shoulder) or -1 (left shoulder). By default, when the camera is enabled, it starts on the right shoulder.
Around the line-numbers noted in the following image will be some configurations that will affect the behavior of the system:
Right now, you can only change the vertical angle limits. These prevent your camera from being able to rotate completely around the x-axis (such that you would be able to see upside down).
For even more customization, the camera system provides a lot of events:
- .ActiveCameraSettingsChanged (receives the new camera setting [string] as argument)
- .CharacterAlignmentChanged (receives the new status [boolean] as argument)
- .MouseStepChanged (receives the new mouse step status [boolean] as argument)
- .ShoulderDirectionChanged (receives the new direction [number] as argument)
By default the camera system is configured such that Q and E will switch shoulders, holding the right mouse button will zoom in, and pressing the control key will step the mouse in or out.
- Added an option for adjusting vertical angle limits
- Added an option to make character rotate with the camera
Update in preparation for support on mobile and console
- Updated code with comments and consistent style
- Combined :StepIn() and :StepOut() functions into :SetMouseStep(steppedIn) along with related flags and events
- Changed how sensitivity works!
- Changed :SetCameraFollow() to :CharacterAlignment() along with related flags and events