The problem I’m trying to solve
An issue with most over-the-shoulder camera systems is that they interfere with existing camera scripts, requiring hacky solutions to get around. What’s more annoying is the lack of control to change the offset of a camera, much less disable/enable it. The last problem with many of these systems is stutter, either with the character or camera, which is so annoying.
My solution to this
I created a third-person camera system that takes advantage of Humanoid.CameraOffset
in addition to spring and vector math to create an intuitive and non-invasive over-the-shoulder third-person camera system. It’s object-oriented, making it easy to control and cleanup! Lastly, this system minimizes that annoying stutter.
You can just plug-n-play! All you have to do is call the constructor to use it.
Here is how it works, when enabled
- Your mouse locks to the center of the screen
- Vector math compares the direction of your camera against the direction of the humanoid root part (let’s call these directions x and y)
- The offset uses vector
Cross
andDot
math to calculate the camera offset of the humanoid based on x and y. - The humanoid root part CFrame is adjusted based on the angle of the camera
Some nice features packaged here
I made sure to include some other nice-to-haves to stand out a bit more!
X-offset adjustments are based on X angle difference
Here is what I mean:
When looking up or down, the camera moves closer to the center of your character. This is particularly useful for third-person shooters to ensure there is no weird behavior when aiming. Regardless, looking up and down from the center of your character makes more sense for most situations.
Camera does not go through parts
Because this uses Humanoid properties to adjust the camera, your camera will not collide with parts and instead move closer to your character if something is blocking your view (like default behavior). This also works with invisicam, if you choose to use that.
Offset adjustments are smoothed
To prevent stutter and motion sickness, springs are implemented to smooth out the offset of the camera.
An OOP approach means easy camera management
You create a third-person camera by calling a constructor and inputting only the character model. The returned object has methods to quickly enable and disable it, as well as a deconstructor to clean up. The object also auto-destroys is the character is destroyed or parented to nil
.
Object methods are:
:Enable()
:Disable()
:Destroy()
The simple code allows for quick and easy changes to cater towards your edge-cases.
Here are some things you can do:
- If you want to remove mouse locking, remove the two lines of code using
UserInputService.MouseBehavior
. The system will still work as intended. - If you want to change the offset, you can easily change the value of the
OFFSET
constant. - You can add additional object methods in a short period of time. For example, if you want one to adjust the offset in real-time, you can in just one or two minutes!
Simple, readable code is important if you need to have it be adjusted to fit specific needs, such as framework compatibility or private features.
Download
Module: ThirdPersonCamera.rbxm (5.2 KB)
Example place: ThirdPersonExample.rbxl (58.7 KB)
How to use this
Here is an example script that you can use! Create a LocalScript
in ReplicatedFirst
and just copy-paste, assuming the module is also in ReplicatedFirst
!
Code
--!nocheck
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ThirdPersonCamera = require(ReplicatedFirst.ThirdPersonCamera)
local function CharacterAdded(Character : Model)
local Camera = ThirdPersonCamera.new(Character) -- Enable third person camera
Character:WaitForChild("Humanoid").Died:Once(function()
Camera:Destroy() -- Upon death, destroy the third person camera, thus resetting to the normal one
end)
end
Player.CharacterAdded:Connect(CharacterAdded)
if Player.Character then
CharacterAdded(Player.Character)
end
And that’s it!
Let me know if you have questions or concerns in the replies below.
You can support me for $1 a month on my Patreon page! Check it out @ iGottic | Game developer and resource creator | Patreon