Making camera rotate without holding right click

I’ve been trying to figure out how to make my 3rd person shoulder cam orbit around the player for the last few hours. I’ve tried to grab the right vector and vector pointing towards the focus point on the XZ plane and try to make a simple orbit but nothing seems to be working. I already have the shoulder cam part working, I just need the camera to orbit around the player without holding right click. If anyone could point me in the direction of a module that someone has made, that would be helpful. Thanks!

2 Likes

I remember having thought really hard about how to do this, but it proved to be quite simple, at least the way I do it.

First, there are two variables to control rotation, yAngle for left and right, and xAngle for up and down. I personally prefer to have them in degrees and to use math.rad() later.

UserInputService.InputChanged:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.MouseMouvement then
        -- Use mouse delta to increase/decrease yAngle and xAngle
    end
end

Then, you need an Update function that is called every RenderStepped. The line where you set the camera cframe will look like this (change variable names as needed):

local yAngle = 0
local xAngle = 0
local yOffset = 1
local xOffset = 2
local distance = 8

-- Put the following line in a function that is called every RenderStepped
camera.CFrame = CFrame.new(humanoidRootPart.Position + Vector3.new(0, yOffset, 0)) 
* CFrame.Angles(0, math.rad(yAngle), 0) 
* CFrame.Angles(math.rad(xAngle), 0, 0) 
* CFrame.new(xOffset, 0, distance) 

This might not work with your previous script, but this is how I do it. Hope this helps

9 Likes

While this does work if you have your camera permanently in this mode, it messes up if you switch between the normal camera functionality and this camera functionality. The player’s camera looks in a different direction whenever you switch to this camera functionality which can be annoying for players in fast-paced combat.

Before switching to this camera functionality, you can get the camera’s current rotation and then set both yAngle and xAngle accordingly.

local active = false

function module.ActivateCamera()
    xAngle, yAngle = camera.CFrame:ToEulerAnglesXYZ()
    camera.CameraType = Enum.CameraType.Scriptable
    active = true
end
1 Like