Can Someone Explain This Head And Torso Follow Camera Script Trigonometry

I have this script i copied from Youtube and i don’t under anything.Can someone explain it to me pls.

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local Angles = CFrame.Angles
local aSin = math.asin
local aTan = math.atan
local MseGuide = true
local TurnCharacterToMouse = false
local HeadHorFactor = 1
local HeadVertFactor = 0.6
local CharacterHorFactor = 0.5
local CharacterVertFactor = 0.4
local UpdateSpeed = 0.5

if TurnCharacterToMouse == true then
	MseGuide = true
	HeadHorFactor = 0
	CharacterHorFactor = 0

	local Character = char
	local Head = Character:WaitForChild("Head")
	local Humanoid = Character:WaitForChild("Humanoid")
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	local IsR6 = (Humanoid.RigType.Value==0)
	local Torso = (IsR6 and Character:WaitForChild("Torso")) or Character:WaitForChild("UpperTorso")
	local Neck = (IsR6 and Torso:WaitForChild("Neck")) or Head:WaitForChild("Neck")
	local Waist = (not IsR6 and Torso:WaitForChild("Waist"))

	local NeckOrgnC0 = Neck.C0
	local WaistOrgnC0 = (not IsR6 and Waist.C0)
	Neck.MaxVelocity = 1/3
		local CameraCF = Camera.CFrame
		if ((IsR6 and Character["Torso"]) or Character["UpperTorso"])~=nil and Character["Head"]~=nil then
			local TorsoLV = Torso.CFrame.lookVector
			local HdPos = Head.CFrame.p
			if IsR6 and Neck or Neck and Waist then
				if Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then
					local Dist = nil;
					local Diff = nil;
					if not MseGuide then
						Dist = (Head.CFrame.Position-CameraCF.Position).magnitude
						Diff = Head.CFrame.Y-CameraCF.Y
						if not IsR6 then
							Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Angles((aSin(Diff/Dist)*HeadVertFactor), -(((HdPos-CameraCF.p).Unit):Cross(TorsoLV)).Y*HeadHorFactor, 0), UpdateSpeed/2)
							Waist.C0 = Waist.C0:lerp(WaistOrgnC0*Angles((aSin(Diff/Dist)*CharacterVertFactor), -(((HdPos-CameraCF.p).Unit):Cross(TorsoLV)).Y*CharacterHorFactor, 0), UpdateSpeed/2)
							Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Angles(-(aSin(Diff/Dist)*HeadVertFactor), 0, -(((HdPos-CameraCF.p).Unit):Cross(TorsoLV)).Y*HeadHorFactor),UpdateSpeed/2)
						local Point = Mouse.Hit.p
						Dist = (Head.CFrame.p-Point).magnitude
						Diff = Head.CFrame.Y-Point.Y
						if not IsR6 then
							Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Angles(-(aTan(Diff/Dist)*HeadVertFactor), (((HdPos-Point).Unit):Cross(TorsoLV)).Y*HeadHorFactor, 0), UpdateSpeed/2)
							Waist.C0 = Waist.C0:lerp(WaistOrgnC0*Angles(-(aTan(Diff/Dist)*CharacterVertFactor), (((HdPos-Point).Unit):Cross(TorsoLV)).Y*CharacterHorFactor, 0), UpdateSpeed/2)
							Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Angles((aTan(Diff/Dist)*HeadVertFactor), 0, (((HdPos-Point).Unit):Cross(TorsoLV)).Y*HeadHorFactor), UpdateSpeed/2)
		if TurnCharacterToMouse == true then
			Humanoid.AutoRotate = false
			HumanoidRootPart.CFrame = HumanoidRootPart.CFrame:lerp(,, HumanoidRootPart.Position.Y, Mouse.Hit.p.z)), UpdateSpeed / 2)
			Humanoid.AutoRotate = true

What i really don’t understand is these lines of code

local Point = Mouse.Hit.p
Dist = (Head.CFrame.p-Point).magnitude
Diff = Head.CFrame.Y-Point.Y

This code is the most youtuber code i’ve ever read, doesn’t have comments, and uses literally ancient API. I haven’t seen “Mouse.Hit.p” since 2011. I would recommend you find a youtuber that actually knows what they are doing. Otherwise, tell me what you want to the camera to do and I will tell you approximately what math you would need to get it.

1 Like

Hello thanks for your reply, what i want to achieve is to move the player’s torso when the player moves the camera, it just rotates with the camera that’s it.

For the example lines you don’t understand, it’s grabbing the mouse’s 3D position (CFrame’s position specifically), which is a Vector3. In the 2nd line they then subtract it with the Head’s position (also Vector3) which gives you the Vector3 difference in which they grab the magnitude of it in order to get the distance between the mouse’s 3D position and Head. In the last line they grab the difference between the head’s Y-axis and the mouse’s Y-axis to determine the height difference. With the distance between the two points, and height (Yaw) difference, you’re able to use math.atan to calculate the radian (then you convert to degrees) needed angle the head towards the height of the mouse.

When the player moves the camera or when they move the mouse? Just making sure because I have seen people ask about the second one.

I want it to move with the camera

Follow the camera exactly or with some dampening? Does it only need to turn side to side or also kinda tilt up and down?

Thank you now i understand why he used math.atan

Follow it exactly and tilt up and down and turn to the sides

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