Custom Camera Is Spazzing Out

I’m currently scripting a custom camera system right now that smoothly moves the camera based on your mouse position, and currently have most of it scripted. However, an issue I currently have is that the camera would spaz out like crazy whenever I move my mouse around extremely fast.

https://gyazo.com/8a48db6f6e7a58902f9f70bc94a9f8ca.mp4

Here is the code that calculates the CFrame that the camera would move to:

	local offset = Values.Offset
	local originCFrame = CFrame.new(rootPart.CFrame.p + Vector3.new(0, 3, 0)) * CFrame.Angles(0, math.rad(Values.AngleX), 0) * CFrame.Angles(math.rad(Values.AngleY), 0, 0)
	local newCFrame = originCFrame + originCFrame:VectorToWorldSpace(Vector3.new(offset.X, offset.Y, offset.Z))
	local goalCFrame = originCFrame + originCFrame:VectorToWorldSpace(Vector3.new(offset.X, offset.Y, 0))
	local finalCFrame = CFrame.new(newCFrame.p, goalCFrame.p)

Right now, the only potential solution I am aware that could fix this issue would be to othernormalize the cframe, however, I am not sure how to properly apply that to the script.

Can you show us the code where you set/interpolate/etc to the new CFrame?

Here. I just use TweenService and have it tween to the finalized CFrame.

TweenService:Create(camera, TweenInfo.new(.1, Enum.EasingStyle.Linear), {CFrame = finalCFrame}):Play()

If two tweens attempt to modify the same property, the initial tween will be cancelled and overwritten by the most recent tween

From the wiki page

2 Likes

Just for clarification what are the offset and AngleX/AngleY variables?

AngleX and AngleY is a variable that changes based off of the mouse delta.

	local delta = UserInputService:GetMouseDelta()
    Values.AngleY = math.clamp(Values.AngleY - delta.Y, -70, 70)
	Values.AngleX = Values.AngleX - delta.X

i believe the issue, could likely lie within how you calculate/use the mouse’s delta. i tested the code you provided and it is going too fast as you showed. i think one way you can fix your problem is by slowing the speed of the camera movement (limiting the mouse delta). i multiplied delta.x by a speed variable in this case .2 and it seemed to slow the camera and somewhat stop the camera from “Spazzing Out” Values.AngleX = (Values.AngleX - (delta.X * .2)) . if you think about the results you got kind of makes since, because of the fact that the delta of the mouse changes or increases the “faster” your move it in any given direction. i haven’t tried normalizing the CFrame, so currently i can not tell you if that works.

I think the error is in the LookAt method, bc the LookAt method isn’t precise at all. I recommend a better way for the result of the FinalCFrame, like this:

local FinalCFrame = CFrame.new(PrimaryPart.Position)*CFrame.Angles(0, X, 0)*CFrame.Angles(Y, 0, 0)*Offset

I just did a fix of your code, if you want take it (Btw don’t use TweenService in a loop, isn’t good at all, a Lerp is better for that):

local Services = {
  PLS = game:GetService("Players");
  RS = game:GetService("RunService");
  UIS = game:GetService("UserInputService")
}

local Client = Services.PLS.LocalPlayer
local Char = Client.Character or Client.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera

local Clamp, D2R = math.clamp, .017453292519943;
local CFNew, CFAngles = CFrame.new, CFrame.Angles;

local function Lerp(A, B, Alpha)
    return A+(B-A)*Alpha
end

local X, Y, Offset = 0, 0, CFNew(3, 1.2, 5)
local RX, RY, Speed = 0, 0, 10
local Delta;

Services.RS:BindToRenderStep("Cam", Enum.RenderPriority.Camera.Value+1, function(DT)
Camera.CameraType = "Scriptable"
Services.UIS.MouseBehavior = "LockCenter"

Delta = Services.UIS:GetMouseDelta()
X, Y = X-Delta.X, Clamp(Y-Delta.Y, -70, 70)
RX, RY = Lerp(RX, X*D2R, DT*Speed), Lerp(RY, Y*D2R, DT*Speed)

   Camera.CFrame = CFNew(Char.PrimaryPart.Position)*CFAngles(0, RX, 0)*CFAngles(RY, 0, 0)*Offset
end)
2 Likes

I had this issue previously and this post from CloneTrooper1019 was the fix.

3 Likes

Thank you so much! Your method worked really well to get the finalized CFrame

@InsufficientFunding that posts help too! Will refer to it in the future.

1 Like