Custom Camera movement script not functioning as intended

recently I’ve been making a custom camera script with smooth movement and a target lock, however after locking onto the target and unlocking when i moved around a bit the pivot (which should remain on the position it ended) shifts away. i got no clue how to fix this besides converting the Angles to a radiance or degree (if possible) and have been trying to figure it out for the past days.

External Media

mouse delta input usage for the camera rotation.

uIS.InputChanged:Connect(function(input, gPE)
	if input.UserInputType == Enum.UserInputType.MouseMovement and not locked then
		xAngle = xAngle-input.Delta.x*0.4
		yAngle = math.clamp(yAngle-input.Delta.y*0.4,-80,80)
	end
end)

the question here is basically, is there a way to convert the CFrame.Angles() of an object (in this case the camera CFrame) to the desired radiance (or degree converted to radiance) so that it matches math.rad(xAngle) or math.rad(yAngle) depending on either x to match or y to match.

Note: xAngle and yAngle gets reset to 0 once locked. assuming that this has nothing to do with the position moving after unlocking since its normal at the first time but once i move around it the offset becomes more.

Thanks in advance.

I assume you are just trying to convert degrees to radians in a cframe, what I would normally do is

local x,y,z = CAMERA_CFRAME:toOrientation()
X y and z are all in radians, to convert it to degrees do
x,y,z = math.deg(x),math.deg(y),math.deg(z)
x,y, and z are now all in degrees.
If I misinterpreted what you are needing here, please let me know.
(I wrote this on mobile, expect something to be misspelled)

i have tried this multiple times in the past this and it didn’t work. i have been thinking and i think my problem might have to be related to the positionizing of the camera’s offset and not the rotation.

i appriciate the effort trough.

Have you tried printing the xAngle and yAngle?

I tried comparing the CFrame.angles(0,math.rad(xangle),0) (same with yangle) and they do match. Printing the xangle and yangle just as it is is useless since it keeps adding up (as shown in the piece of code). But the problem here might be that the position shifts due to a startCFrame variable position which somehow won’t change back to the position it’s ended on. I’ll send some more code of the script as soon as i can.

local rootPart = Character:WaitForChild("HumanoidRootPart")

local startCFrame

startCFrame = CFrame.new(rootPart.CFrame.p)*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)

cameraCFrame = startCFrame+startCFrame:vectorToWorldSpace(cameraPos)

if lockamounts > 0 then
    cameraCFrame = rootPart.CFrame+startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X, cameraPos.Y, cameraPos.Z))
end
if locked and target ~= nil then
    baseframe = CFrame.new((rootPart.CFrame+rootPart.CFrame:vectorToWorldSpace(cameraPos)).p, target.Position)
    xAngle = 0
    yAngle = 0
else
    baseframe = baseframe - baseframe.p + cameraCFrame.p
end
local newCFrame = baseframe * CFrame.fromEulerAnglesYXZ(math.rad(yAngle), math.rad(xAngle), 0)

this is how the camera script works. the newCFrame is the cframe that’s applied to the camera. i hope this is enough information.

Have you tried using something like Camera:WorldToViewportPoint to change xAngle and yAngle?

local ScreenPos, isOnScreen = camera:WorldToViewportPoint(target.Position)

local RelativePos = Vector2.new(ScreenPos.X,ScreenPos.Y) - camera.ViewportSize * 0.5
-- it might be reversed, I don't know.

xAngle, yAngle = xAngle + RelativePos.X, yAngle + RelativePos.Y

So 4 notes from this:

  • I don’t think you have to convert anything between radians and degrees because of your setup, but you might have to test for that anyway.
  • I don’t know if you can use this to lock onto something off-screen, since WorldToViewportPoint returns a bool saying if the point is on screen or not.
  • You will probably have to use this function every frame.
    – You should bind this to a key when testing instead of actually using it every frame just to see if the camera is moving correctly.
  • If you want to make the angles move smoothly to that point, you can convert xAngle and yAngle to NumberValues and use TweenService.

I hope this helps!

I hope my interpretation of the question wasn’t too far off…

i appriciate everyone here for the effort but i managed to fix the issue. it turned out it wasn’t a rotational issue at all. i just had to redo the StartCFrame in the if lockamounts > 0 then statement in order to keep it on place.