Making player aim where they're looking even with rotated/angled torso and shoulders by animation

I want to achieve the characters head and arms rotating where the player is looking no matter how is the torso rotated.

It works fine when: the torso isnt rotated by animation so its looking forward, the gun is being held and aimed in the direction where the torso is facing if that makes sense.

I suck at this math so i tried getting help from AI and checked different posts, didnt find a solution

video of whats happening:

 local function getNeck(character, humanoid)
	if not character or not humanoid then return end
	if humanoid.RigType == Enum.HumanoidRigType.R6 then
		local torso = character:FindFirstChild("Torso")
		return torso and torso:FindFirstChild("Neck")
	else
		local head = character:FindFirstChild("Head")
		return head and head:FindFirstChild("Neck")
	end
end

local function getShoulders(character, humanoid)
	if not character or not humanoid then return end
	if humanoid.RigType == Enum.HumanoidRigType.R6 then
		local torso = character:FindFirstChild("Torso")
		return torso and torso:FindFirstChild("Left Shoulder"), torso and torso:FindFirstChild("Right Shoulder")
	else
		local leftUpperArm = character:FindFirstChild("LeftUpperArm")
		local rightUpperArm = character:FindFirstChild("RightUpperArm")
		return leftUpperArm and leftUpperArm:FindFirstChild("LeftShoulder"), rightUpperArm and rightUpperArm:FindFirstChild("RightShoulder")
	end
end

local function getDefaultNeckCFrame(humanoid)
	if humanoid.RigType == Enum.HumanoidRigType.R6 then
		return CFrame.new(0, 1, 0, -1, 0, 0, 0, 0, 1, 0, 1, -0)
	else
		return CFrame.new(-5.96046448e-08, 0.800017118, 1.1920929e-07, 1, 0, 0, 0, 1, 0, 0, 0, 1)
	end
end

local function getDefaultShoulderCFrames(humanoid)
	if humanoid.RigType == Enum.HumanoidRigType.R6 then
		return CFrame.new(-1, 0.5, 0) * CFrame.Angles(0, -1.55, 0), CFrame.new(1, 0.5, 0) * CFrame.Angles(0, 1.55, 0)
	else
		return CFrame.new(-1, 0.56301713, 1.1920929e-07, 1, 0, 0, 0, 1, 0, 0, 0, 1), CFrame.new(0.99999994, 0.56301713, 1.1920929e-07, 1, 0, 0, 0, 1, 0, 0, 0, 1)
	end
end

local function getShoulderAimCFrame(player, humanoid)
	local defL, defR = getDefaultShoulderCFrames(humanoid)
	local defNeck = getDefaultNeckCFrame(humanoid)
	local isAiming = player == LocalPlayer and (WeaponData[player] and WeaponData[player].Aiming) or player.Character:GetAttribute("Aiming")
	local angle = isAiming and math.rad(10) or 0
	local aimTilt = CFrame.Angles(0, angle, 0)
	local stnL, stnR = defL:Inverse() * defNeck, defR:Inverse() * defNeck
	return stnL * aimTilt * stnL:Inverse(), stnR * aimTilt * stnR:Inverse()
end

function CharacterLookController.updatePlayer(player, lookDirection, headLook, armsLook, lerpWeight, dt)
	local character = player.Character
	if not character then return end
	local humanoid = character:FindFirstChild("Humanoid")
	local hrp = character:FindFirstChild("HumanoidRootPart")
	local torso = character:FindFirstChild("Torso") or character:FindFirstChild("UpperTorso")
	local neck = getNeck(character, humanoid)
	local lS, rS = getShoulders(character, humanoid)
	if not (humanoid and neck and lS and rS and hrp and torso) then return end
	local moveData = DirectionalMovement.GetOffsets(character, dt)
	local rootJoint = hrp:FindFirstChild("RootJoint")
	if rootJoint and moveData then 
		rootJoint.C0 = moveData.RootC0 
	end
	local localLookDir = torso.CFrame:VectorToObjectSpace(workspace.CurrentCamera.CFrame.LookVector)
	local verticalAngle = math.asin(localLookDir.Y)
	local neckBase = getDefaultNeckCFrame(humanoid)
	local moveNeck = moveData and moveData.NeckTilt or CFrame.new()
	local targetNeckC0 = neckBase * moveNeck
	if headLook then
		local isAiming = player == LocalPlayer and (WeaponData[player] and WeaponData[player].Aiming) or character:GetAttribute("Aiming")
		local aimTilt = isAiming and math.rad(15) or 0


		local lookCFrame = CFrame.Angles(humanoid.RigType == Enum.HumanoidRigType.R6 and -verticalAngle or verticalAngle, -aimTilt, 0)
		targetNeckC0 = neckBase * moveNeck * lookCFrame
	end
	neck.C0 = neck.C0:Lerp(targetNeckC0, lerpWeight)


	local defL, defR = getDefaultShoulderCFrames(humanoid)
	local moveShoulder = moveData and moveData.ShoulderTilt or CFrame.new()
	local targetL, targetR = defL * moveShoulder, defR * moveShoulder

	if armsLook then
		local AimL, AimR = getShoulderAimCFrame(player, humanoid)

		local transL, transR
		if humanoid.RigType == Enum.HumanoidRigType.R6 then
	
			transL, transR = CFrame.Angles(0, 0, -verticalAngle), CFrame.Angles(0, 0, verticalAngle)
		end

		targetL = defL * moveShoulder * AimL * transL
		targetR = defR * moveShoulder * AimR * transR
	end

	lS.C0 = lS.C0:Lerp(targetL, lerpWeight)
	rS.C0 = rS.C0:Lerp(targetR, lerpWeight)
end

Any help is appreciated im really stuck on this

I think, you can solve this problem for using The “Mouse”.

Like This:

local Mouse = plr:GetMouse()

local function MakeFollowPlayerTorsoToMousePosition() -- IDK FUNCTION NAME LOL
--Idk
--Idk
--Idk
--Idk
end

Also your script is nice
no way that i can do it

my english is bad sorry lol

(But I think this plan only work when Locking the Mouse, lol)

1 Like

here’s how my game does it:
rotate right shoulder c0 so that it points the same direction as the torso
apply whatever rotations
apply inverse of first rotation
calculate left shoulder c0 from right shoulder c0

local LEFT_ARM_OFFSET = (CFrame.new(-0.5, 0.5, 0) * CFrame.Angles(0, math.pi / 2, 0)):Inverse() * CFrame.new(-3, 0, 0) * CFrame.new(0.5, 0.5, 0)

local _, _, torsoZ = self.RootJoint.Transform:ToEulerAnglesXYZ()
local dir = self.Root.CFrame:ToObjectSpace(mouse.Origin).LookVector.Unit

local _, _, headZ = self.Neck.Transform:ToEulerAnglesXYZ()

self.RightShoulder.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(0, -torsoZ, 0) * CFrame.Angles(math.asin(dir.Y), 0, 0) * CFrame.Angles(0, torsoZ, 0) * CFrame.Angles(0, math.pi / 2, 0)
self.LeftShoulder.C0 = self.RightShoulder.C0 * LEFT_ARM_OFFSET * CFrame.Angles(0, -math.pi / 2, 0)

self.Neck.C0 = CFrame.new(0, 1, 0) * CFrame.Angles(0, headZ, 0) * CFrame.Angles(math.asin(dir.Y), 0, 0) * CFrame.Angles(0, -headZ, 0) * CFrame.Angles(math.pi / 2, -math.pi, 0)
3 Likes

Thank you so much! this works perfectly like i wanted.

Glad you found a way, good luck, I’m keeping that code. Still working on it a bit anyways.

1 Like

interesting, ill have a look at it

2 Likes

blud stop leaking stuff from gta 6 source code :sob:

1 Like

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