Torso Follow mouse not smooth

Hello, I’m having a little issue.

When the mouse goes to different objects the torso snaps with it.

ezgif.com-gif-maker

I basically used this code by: @CleverSource

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 Neck = Head:WaitForChild("Neck")

local Torso = Character:WaitForChild("UpperTorso")
local Waist = Torso:WaitForChild("Waist")

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

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

Neck.MaxVelocity = 1/3

RunService.RenderStepped:Connect(function() 
	local CameraCFrame = Camera.CoordinateFrame
	
	if Character:FindFirstChild("UpperTorso") 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
				
				Neck.C0 = Neck.C0:lerp(NeckOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1, 0), 0.5 / 2)
				Waist.C0 = Waist.C0:lerp(WaistOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5, 0), 0.5 / 2)
			end
		end
	end	
end)

Does anyone know how to make it less snappy? Or not at all? Any help is appreciated!

2 Likes

Try using the delta time parameter of RenderStepped to effectively dampen your animation on the Neck & Wait lerp.

To note - I’ve increased t he neck and waist multiplication from 1 to 30 and 0.5 to 15 as it will probably be very slow otherwise, adjust accordingly.

RunService.RenderStepped:Connect(function(dt) 
	local CameraCFrame = Camera.CoordinateFrame
	
	if Character:FindFirstChild("UpperTorso") 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
				
				Neck.C0 = Neck.C0:lerp(NeckOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 30 * dt, 0), 0.5 / 2)
				Waist.C0 = Waist.C0:lerp(WaistOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 15 * dt, 0), 0.5 / 2)
			end
		end
	end	
end)

Ah I’m actually using heartbeart in my own script.

Let me show the current script I have.

Tool.Equipped:Connect(function()
	mouse.Icon = "http://www.roblox.com/asset/?id=6990504894"
	char.Humanoid.AutoRotate = false
	BG.Parent = Root
	OTS:Enable()
	
	local function UpdateBody()
		local camLv = cam.CFrame.lookVector
		local camRotation = math.atan2(-camLv.X, -camLv.Z)
		BG.CFrame = CFrame.Angles(0, camRotation, 0)
		
		local Point = mouse.Hit.p

		local Distance = (Head.CFrame.p - Point).magnitude
		local Difference = Head.CFrame.Y - Point.Y
		
		local TorsoLookVector = Torso.CFrame.lookVector
		local HeadPosition = Head.CFrame.p
		Neck.C0 = Neck.C0:lerp(NeckOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1, 0), 1)
		Waist.C0 = Waist.C0:lerp(WaistOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 1), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5, 0), 1)
	end
	Heartbeat = RunService.Heartbeat:Connect(UpdateBody)
	
end)
1 Like

one solution i came up with this is to keep the point to a fixed distance to not make it snap

function getPoint()
     local r=Camera:ViewportPointToRay(PlayerMouse.X,PlayerMouse.Y,1337);
     return r.Origin+r.Direction;
end;

its less accurate but still works without making the interpolation take longer
final code:

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 Neck = Head:WaitForChild("Neck")

local Torso = Character:WaitForChild("UpperTorso")
local Waist = Torso:WaitForChild("Waist")

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

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

Neck.MaxVelocity = 1/3

function getPoint()
	local r=Camera:ViewportPointToRay(PlayerMouse.X,PlayerMouse.Y,1337);
	return r.Origin+r.Direction;
end;

RunService.RenderStepped:Connect(function() 
	local CameraCFrame = Camera.CoordinateFrame

	if Character:FindFirstChild("UpperTorso") 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 = getPoint();

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

				Neck.C0 = Neck.C0:lerp(NeckOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1, 0), 0.5 / 2)
				Waist.C0 = Waist.C0:lerp(WaistOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5, 0), 0.5 / 2)
			end
		end
	end	
end)
5 Likes

Both heartbeat and renderstepped have the same delta parameter, but whatever works.

3 Likes

Quick question, how do I make animated arms follow mouse actually? Or in this video arms are animated to M4 with script? Or maybe its priority? For torso it works but arms are like stuck in air.

1 Like