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
:ToObjectSpacedo exactly? - What is
lookVectorused 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!