Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i turn the player's torso with the player's head?

i am currently making a head movement script on a custom character. it works perfectly but it can turn its head all the way backwards. how can i make it so that the torso turns when the head angle exceeds 45 degrees like in minecraft? here is the script by the way

local uis = game:GetService("UserInputService")
local char = script.Parent
local head = char.Head
local cam = workspace.Camera
local viewport = game.ReplicatedStorage.Viewport:Clone()
local hand = char["Right Arm"]:Clone()
local neck = char.Torso:FindFirstChild("Neck")
local torso = char.Torso
local origin = neck.C0
hand.Parent = viewport
viewport.Parent = head
hand.CFrame = viewport.Arm.CFrame
hand.Anchored = true
viewport.Arm:Destroy()
cam.CameraSubject = head

function view()
	hand.LocalTransparencyModifier = 0
	viewport:SetPrimaryPartCFrame(cam.CFrame)
	for i,v in pairs(char:GetChildren()) do
		if v:IsA("Part") and v ~= head then
			v.LocalTransparencyModifier = 0
		end
	end
end

function turn()
	--i guess this is where i make the torso turn
end

function headmovement()
	local adjust = torso.CFrame:ToObjectSpace(cam.CFrame)
	local angle = adjust-adjust.Position
	neck.C0 = origin*angle
end

function update()
	turn()
	view()
	headmovement()
end

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

1 answer

You can use math.clamp for this.

math.clamp(value,min,max) is a way to 'clip' numbers. For example, writing math.clamp(46,0,45) will return 45, since 45 is the maximum value.

To use math.clamp, simply write if math.clamp(HEAD_TURN,-45,45) >= 45 then or you can use if math.clamp(HEAD_TURN,-45,45) <=-45 then.

The value can not go under -45 and can't go over 45

This discussion is preserved as it was. New answers, comments, and votes are closed.