Help with rotating arm in correct angle

Hi, I’m making a game and I added head looking to my game from okeanskiy’s tutorial, but I’m now trying to rotate the arm to where the head is facing - 90 degrees. I’m having issues with this, since I don’t know how to work this.

This is code for the headlook and right arm rotation

local players = game:GetService("Players")
local lp = players.LocalPlayer
local char =  lp.Character or lp.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")
local run = game:GetService("RunService")
local cam = workspace.CurrentCamera
local torso = char:FindFirstChild("Torso")
local neck = torso:WaitForChild("Neck")
local rightarm = torso:WaitForChild("Right Shoulder")

local y = neck.C0.Y
local rep = game:GetService("ReplicatedStorage")

local lastTick = tick()

run.RenderStepped:Connect(function(deltaTime)
	local camdirec = root.CFrame:ToObjectSpace(cam.CFrame).LookVector
	if neck then
		
		-- Rotate neck (works fine)
		
		neck.C0 = CFrame.new(0,y,0) * CFrame.Angles(0,math.rad(180),0) * CFrame.Angles(0,-camdirec.X,0) * CFrame.Angles(-camdirec.Y,0,0)
		neck.C0 = neck.C0 * CFrame.Angles(math.rad(-90),0,0)
		if tick() - lastTick >= 0.9 then
			lastTick = tick()
			rep.Events.NeckCF:FireServer(neck.C0)
		end
		
		
		-- Problem starts here
		-- Rotate arm (head looking straight forward should point the arm down)
		-- When the head looks up (lets say straight up), the arm should point straight
		
		local x, y, z = neck.C0:ToEulerAnglesXYZ()
		rightarm.C0 = CFrame.new(rightarm.C0.Position) * CFrame.fromEulerAnglesXYZ(0, math.rad(90), -x)
	end
end)

This is how it looks when the code is ran:


w
This is how I want it to look:
RobloxStudioBeta_yUOHSwo1ZY
RobloxStudioBeta_pYuAQAttcU

looks like you’ve got it generally right, just need to swap a positive/negative & change a base value

something like this might work (i re-ordered the code a bit)

local x, y, z = neck.C0:ToEulerAnglesXYZ()
local baseY = math.rad(90)
local baseRotation = math.rad(90) -- Change this around ?
rightarm.C0 =  CFrame.fromEulerAnglesXYZ(0, baseY, baseRotaton + x) + rightarm.C0.Position

(cframe math takes a lot of brainpower, cases like this I’d brute force)

1 Like

Thank you so much, this worked perfectly. I knew I had to swap the value to make a positive, but I couldn’t generally figure it out, but you gave me the solution. Thanks again!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.