Animations + Rotate character to mouse = Chaos

Hi, I currently have a script that rotates the player’s character to where the camera/mouse is facing. By itself, this code works great and exactly how I want it to! But when you introduce animations that add complex dynamics (in this case rotating the torso 30 degrees, gun holding animation), the script seems to completely break!

I had to cut this video short because of the upload limit, but as you can see, about 3 seconds in when the animation plays, the script completely breaks!

Does anyone have any idea on how to resolve this? Any help is appreciated!

PS: This script works alone in R6, so feel free to test it.

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local PlayerMouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local Character = Player.Character or Player.CharacterAdded:Wait()

local Head = Character:WaitForChild("Head")
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Torso = Character:WaitForChild("Torso")
local Neck = Torso:WaitForChild("Neck")
local Waist = HumanoidRootPart:WaitForChild("RootJoint")
local RHip = Torso:WaitForChild("Right Hip")
local LHip = Torso:WaitForChild("Left Hip")

local LHipOriginC0 = LHip.C0
local RHipOriginC0 = RHip.C0

local NeckOriginC0 = Neck.C0
local WaistOriginC0 = Waist.C0

Neck.MaxVelocity = 1/3

RunService.RenderStepped:Connect(function() 

	local CameraCFrame = Camera.CoordinateFrame

	if Character:FindFirstChild("Torso") and Character:FindFirstChild("Head") then

		local TorsoLookVector = Torso.CFrame.lookVector
		local HeadPosition = Head.CFrame.p

		if Neck and Waist then
			if Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then

				local Point = PlayerMouse.Hit.p

				local Distance = (Head.CFrame.p - Point).magnitude
				local Difference = Head.CFrame.Y - Point.Y

				local intensity = 2

				local goalNeckCFrame = CFrame.Angles(-(math.atan(Difference / Distance) * 0.5 * intensity), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1 * intensity, 0)

				Neck.C0 = Neck.C0:lerp(goalNeckCFrame * NeckOriginC0, 0.5 / 2).Rotation + NeckOriginC0.Position

				local xAxisWaistRotation = -(math.atan(Difference / Distance) * 0.5 * intensity)
				local yAxisWaistRotation = (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5 * intensity
				local rotationWaistCFrame = CFrame.Angles(xAxisWaistRotation, yAxisWaistRotation, 0)
				local goalWaistCFrame = rotationWaistCFrame * WaistOriginC0

				Waist.C0 = Waist.C0:lerp(goalWaistCFrame, 0.5 / 2).Rotation + WaistOriginC0.Position + Vector3.new(0,0,xAxisWaistRotation)

				local currentLegCounterCFrame = Waist.C0 * WaistOriginC0:Inverse()
				local legsCounterCFrame = currentLegCounterCFrame:Inverse()
				
				RHip.C0 = legsCounterCFrame * RHipOriginC0
				LHip.C0 = legsCounterCFrame * LHipOriginC0
			end
		end
	end	
end)

Bump
‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

Fixed! For anyone who runs into this problem in the future, here ya go:

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local PlayerMouse = Player:GetMouse()

local Camera = workspace.CurrentCamera

local Character = Player.Character or Player.CharacterAdded:Wait()

local Head = Character:WaitForChild("Head")

local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local Torso = Character:WaitForChild("Torso")
local Neck = Torso:WaitForChild("Neck")

local Waist = HumanoidRootPart:WaitForChild("RootJoint")

local RHip = Torso:WaitForChild("Right Hip")
local LHip = Torso:WaitForChild("Left Hip")

local LHipOriginC0 = LHip.C0
local RHipOriginC0 = RHip.C0

local NeckOriginC0 = Neck.C0
local WaistOriginC0 = Waist.C0

Neck.MaxVelocity = 1/3

RunService.RenderStepped:Connect(function() 

	local CameraCFrame = Camera.CoordinateFrame

	if Character:FindFirstChild("Torso") and Character:FindFirstChild("Head") then

		local TorsoLookVector = HumanoidRootPart.CFrame.lookVector
		local HeadPosition = Head.CFrame.p

		if Neck and Waist then
			if Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then

				local AnimationAngleOffset = Waist.CurrentAngle

				local Point = PlayerMouse.Hit.p

				local Distance = (Head.CFrame.p - Point).magnitude
				local Difference = Head.CFrame.Y - Point.Y

				local torsoIntensity = 1.5
				local headIntensity = 1

				local verticalAngle = math.atan(Difference / Distance)
				local scaledVerticalAngle = - (verticalAngle * 0.5 * headIntensity)
				local directionVector = (HeadPosition - Point).Unit
				local crossProduct = directionVector:Cross(TorsoLookVector)
				local horizontalAngle = crossProduct.Y * 1 * headIntensity

				local goalNeckCFrame = CFrame.Angles(scaledVerticalAngle, horizontalAngle, -AnimationAngleOffset * verticalAngle / (math.pi/2) --[[ Adjust the vertical angle based on AnimationAngleOffset]])

				Neck.C0 = Neck.C0:lerp(goalNeckCFrame * NeckOriginC0, 0.5 / 2).Rotation + NeckOriginC0.Position

				local xAxisWaistRotation = -(math.atan(Difference / Distance) * 0.5 * torsoIntensity)
				local yAxisWaistRotation = (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5 * torsoIntensity
				local rotationWaistCFrame = CFrame.Angles(xAxisWaistRotation, yAxisWaistRotation, 0)
				local goalWaistCFrame = rotationWaistCFrame * WaistOriginC0

				Waist.C0 = Waist.C0:lerp(goalWaistCFrame, 0.5 / 2).Rotation + WaistOriginC0.Position + Vector3.new(0,0,xAxisWaistRotation)

				local LegsAnimationCounter = (Waist.C0 * CFrame.fromEulerAnglesXYZ(0,0,AnimationAngleOffset))

				local currentLegCounterCFrame = LegsAnimationCounter * WaistOriginC0:Inverse()
				local legsCounterCFrame = currentLegCounterCFrame:Inverse()

				RHip.C0 = legsCounterCFrame * RHipOriginC0
				LHip.C0 = legsCounterCFrame * LHipOriginC0
			end
		end
	end	
end)

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