Help With Camera/CFrames

Hello, developers! I am working on a script for head adjusting according to camera movement, and I got stuck, so I decided to come to YouTube for help.

I came across this script that I do not understand, at all;

local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer

local Character = Player.Character
local Root = Character:WaitForChild("HumanoidRootPart")
local Neck = Character:FindFirstChild("Neck", true)
local YOffset = Neck.C0.Y

local CFNew, CFAng = CFrame.new, CFrame.Angles
local asin = math.asin

game:GetService("RunService").RenderStepped:Connect(function()
	local CameraDirection = Root.CFrame:toObjectSpace(Camera.CFrame).lookVector
	if Neck then
		Neck.C0 = CFNew(0, YOffset, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(CameraDirection.x)) * CFAng(-asin(CameraDirection.y), 0, 0)
	end
end)

(From this video by okeanskiy: https://www.youtube.com/watch?v=HjfjWySxRRA&ab_channel=okeanskiy)

Although it works great, I would like to understand how this code works so I can be more independent in the future. Here are some of my questions:

  • How do people find formulas like this; do you just guess and check?
  • What does arc sine do exactly?
  • What does :ToObjectSpace do exactly?
  • What is lookVector used for?
  • What is a unit vector/normalized copy?

Also, I’d like to know how I could adjust my script to work better:

local cam = workspace.CurrentCamera

local plr = game:GetService("Players").LocalPlayer

local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")

local neck = char:FindFirstChild("Neck", true)

local y = neck.C0.Y


game:GetService("RunService").RenderStepped:Connect(function()

	if neck then
		neck.C0 = CFrame.new(0, y, 0) * CFrame.Angles((cam.CFrame.UpVector.Unit * 3) - math.rad(135), math.rad(-180), 0)
	end
end)

Once again thank you all for helping! I really look forward to learning from your responses!

How do people find formulas like this; do you just guess and check?

Graphics classes in university help a lot with designing things like this. Linear algebra classes and an understanding of vector math in 3D space also helps.

What does arc sine do exactly?

Arcsine is the inverse of sine. It gives the angle associated with the given sine value.

What does :ToObjectSpace do exactly?

Object space is essentially what the world looks like from your object’s point of view. Tilt a part in studio, then try to translate it. You’ll notice that its coordinate GUI has changed. Moving the object up may now also move it to the side from your point of view (world space). :ToObjectSpace() gives us access to this in the script.

What is lookVector used for?

Look at an object in your room. Imagine a laser beam shooting from the center of your head to that object. The look vector is a way to define that laser beam.

What is a unit vector/normalized copy?

A unit vector is a vector with a magnitude (length) of 1. Imagine the laser beam from the previous example. Now imagine it with a length of 1 meter. This makes calculations easier down the road.

Also, I’d like to know how I could adjust my script to work better:

My recommendation would be to pull out a graphing calculator and some paper. Try reverse engineering the example you found (plug in numbers for specific parts of the formula) and see what happens. Then, once you understand how the numbers get manipulated, try implementing something similar in your code.

They are not typically found through guesswork. They are based on mathematical and geometric principles and are often used in computer graphics

A mathematical function that calculates the arcsine of a value. The arcsine is the inverse trigonometric function of sine. If you know trigonometry it will make sense.

It’s used to convert a CFrame from world space (global coordinates) to the object’s local space (coordinates relative to the object).

Get the direction in which a CFrame is pointing.

A unit vector, is a vector with a magnitude of 1. Magnitude is the length of the vector if you didn’t know.

Find actual formulas for such a thing (Wikipedia will certainly have them and full explanation on them) and implement them yourself.