Explanation to Cos & Sin Camera Movements?

Hello people,

I’ve been looking at a lot of scripts to try and figure out why they work and how they work. So far I have not been doing so well.

I just got back onto roblox after a long time of taking a break. I am still… Ok at roblox but I lost all memory of my advanced memory, which I barely knew when I was actively working.

I’ll show you a script and show you why I’m confused.

local player = game.Players.LocalPlayer
local character = workspace:WaitForChild(player.CharacterAdded:Wait().Name)
local camera = workspace.CurrentCamera
local running = false
local speed = character:WaitForChild("Humanoid").WalkSpeed / 2
local RunService = game:GetService("RunService")
local Pi = 3.1415926
local Rot = 3.1415926 / 2

    if WalkSpeed > 0.1 then
        running = true
        running = false

while true do
    fps = (camera.CoordinateFrame.p - character.Head.Position).Magnitude
    if running == true then
        Rot = Rot + character.Humanoid.WalkSpeed / 92
    if Rot >= Pi * 2 then
        Rot = 0
    camera.CoordinateFrame = camera.CoordinateFrame * CFrame.new(math.cos(Rot) / speed, math.sin(Rot * 2) / (speed * 2), 0) * CFrame.Angles(0, 0, math.sin(Rot - Pi * 1.5) / (speed * 20))

What do COS and SIN equal to? What do they accomplish together? Why do we use PI in this? The last line of code confuses me the most, I understand that it makes everything work and makes the camera move but… Why do all these go in their places? CoordinateFrame? What is all this?

So… Yea I am VERY confused. There are lots of other things I wonder about too, like “What things do I need to know to create (this)”? (Stuff like Custom FPS rigs, round start and ends, how to properly learn raycast without it being super confusing, detecting vertices on a part to detect when you are looking at it or not, etc. I could come up with MUCH MORE.)

But I am just mainly focused on Cos and Sin explanations for now. Sources would very much be appreciated. But I accept any form of explanation. God bless you all.

Always check the create.roblox.com site for explanations of items in Roblox.
You must have an old script there, since camera.CoordinateFrame has been deprecated and replaced by camera.CFrame
math | Roblox Creator Documentation explains all the functions of math.(whatever)

Sin and Cos are mathematical functions of angles.

1 Like

The last line of code is probably the most confusing because it contains everything in one giant line.

I believe you can investigate further on your own for the best understanding if you split it up into step by step components:

--I know that the CFrame is CFrame.new(x,y,z) which contains only position.
--Hence this controls the position only
--This position is added onto the current camera CFrame with multiplication camera.CoordinateFrame * positionCFrameModifier
--Moves in left and right x axis = math.cos(Rot) / speed
--y axis = math.sin(Rot * 2) / (speed * 2)
local positionCFrameModifier = CFrame.new(math.cos(Rot) / speed, math.sin(Rot * 2) / (speed * 2), 0) 

--I know that the CFrame is CFrame.Angles(x,y,z)
--Hence this controls the rotationonly
--This position is added onto the current camera CFrame with multiplication camera.CoordinateFrame * positionCFrameModifier * addedRotationalCFrameModifier 
local addedRotationalCFrameModifier =  CFrame.Angles(0, 0, math.sin(Rot - Pi * 1.5) / (speed * 20))

camera.CoordinateFrame = camera.CoordinateFrame * positionCFrameModifier * addedRotationalCFrameModifier 

Even I also don’t fully know this script as this is my first time seeing it. The next step I would go for is to investigate the Rot variable and probably try to graph it out.

One example is for the X axis the formula is math.cos(Rot) / speed.

Rot seems to continuously increase in a loop during running from 0 to 2 pi, this will be the X axis of the graph. Speed is a constant 16/2 = 8

local speed = character:WaitForChild("Humanoid").WalkSpeed / 2

plotting it out we can get the added left and right movement of the camera.

Seems like it will add movement in the + X axis first then go back.

Pi is probably used to shift the graph and change the phase of the sin curve and change the timing in which the peaks occur

for example for math.sin(Rot - Pi * 1.5) / (speed * 20) in the Z axis CFrame angles. For an example I would reverse the sign by adding the pi instead and make it possible move left instead of right initially, or make it rotate clockwise initially instead of anti clockwise.


Cos and Sin are functions from the math library in lua. They are used to calculate the x and y position of a point on the circumference of a circle, with the circle’s center being the origin. PI is used to convert the angle into radians, which is the unit used to measure angles in lua. The last line of code is used to move the camera around the character, using the character’s WalkSpeed and the angle determined by the Cos and Sin functions.

1 Like