I want a player’s camera to orbit around a position. Rotating around the Y axis works fine, but rotating around either the Z or X axis causes abrupt and brief jerks to happen twice per orbit.
Code with jerking that rotates around X:
local Services = {
Run = game:GetService("RunService"),
Workspace = game:GetService("Workspace"),
}
local Configuration = {}
Configuration.Target = Services.Workspace.Part
Configuration.OrbitRadius = 10
Configuration.FramesPerOrbit = 360
local Camera = Services.Workspace.Camera
local SpinAngle = 0
local function UpdateCamera()
SpinAngle = (SpinAngle + ((2 * math.pi) / Configuration.FramesPerOrbit)) % (2 * math.pi)
local Offset = CFrame.new(0, math.sin(SpinAngle) * Configuration.OrbitRadius, math.cos(SpinAngle) * Configuration.OrbitRadius)
local Position = Offset:ToWorldSpace(Configuration.Target.CFrame).Position
Camera.CFrame = CFrame.new(Position, Configuration.Target.Position)
end
Camera.CameraType = Enum.CameraType.Scriptable
Camera.Focus = Configuration.Target.CFrame
Services.Run.Stepped:Connect(UpdateCamera)
Code without jerking that rotates properly around the Y axis:
local Services = {
Run = game:GetService("RunService"),
Workspace = game:GetService("Workspace"),
}
local Configuration = {}
Configuration.Target = Services.Workspace.Part
Configuration.OrbitRadius = 10
Configuration.FramesPerOrbit = 360
local Camera = Services.Workspace.Camera
local SpinAngle = 0
local function UpdateCamera()
SpinAngle = (SpinAngle + ((2 * math.pi) / Configuration.FramesPerOrbit)) % (2 * math.pi)
local Offset = CFrame.new(math.sin(SpinAngle) * Configuration.OrbitRadius, 0, math.cos(SpinAngle) * Configuration.OrbitRadius)
local Position = Offset:ToWorldSpace(Configuration.Target.CFrame).Position
Camera.CFrame = CFrame.new(Position, Configuration.Target.Position)
end
Camera.CameraType = Enum.CameraType.Scriptable
Camera.Focus = Configuration.Target.CFrame
Services.Run.Stepped:Connect(UpdateCamera)
I should be able to just solve this as a 2D problem, where I can replace X and Y with any two axis. Why can’t I?