So, I’ve been trying to program the player’s head moving with the camera’s movements, and I’ve been struggling a little bit.
Basically, every solution I’ve tried just simply hasn’t worked - either my head goes thousands of studs out, or it doesn’t work properly, or just errors, an example of which is below. (Yeah, I get the error, by the way - this was just the last bit of code I actually made myself)
I’ve tried asking GPT just to see if it has any input, and looked at a really old post about a similar thing, but it was with a R6 rig, and didn’t really make any sense to me.
I’m normally decent with programming, but I’ve never really worked with Necks (or joints in general), so don’t mind the terrible code below:
-- >> This is the code that's erroring a bunch; I don't really have anything of the other ones that partially worked, for reference. << --
local player = game.Players.LocalPlayer;
local character = player.Character;
local neck = character.Head.Neck;
local origc0 = neck.C0
local camera = workspace.CurrentCamera
game:GetService("RunService").Heartbeat:Connect(function()
local angle = camera.CFrame.LookVector;
neck.C0 = neck.C0:ToObectSpace(CFrame.Angles(math.rad(angle.X), math.rad(angle.Y), math.rad(angle.Z)))
end)
Does anyone have an idea as to how I should proceed or what I’m doing wrong? I know I’m doing something wrong, I just don’t know how to fix it.
Hi, CFrame.LookVector only return a number from -1 to 1 so that won’t work with angle. I found a fix with just getting the rotation and calculating that with the original offset.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local head = character:WaitForChild("Head")
local upperTorso = character:WaitForChild("UpperTorso")
local neck = head:WaitForChild("Neck")
-- store orignal offset
local origC0 = neck.C0
game:GetService("RunService").RenderStepped:Connect(function()
local camCF = camera.CFrame
local charCF = upperTorso.CFrame
local relativeRotation = charCF:ToObjectSpace(camCF) -- get relative CFrame
local _, y, z = relativeRotation:ToEulerAnglesYXZ() -- get rotation
local newC0 = origC0 * CFrame.Angles(0, y, z)
-- apply rotation
neck.C0 = newC0
end)
Hmm, interesting - this works, thank you! I didn’t know LookVector only returned between -1 and 1, that’s interesting.
This does work, though, thank you! I’m newer to joints, and this is one of those things I’ve just never ended up dabbling in.
I’ve also always seen ToEulerAngles, but didn’t think about it.