Making the torso lean to the humanoids move direction

im currently stumped on making the players torso lean into the humanoids move direction
ive tried using bodygyro, motor6d.Transform, motor6d.C0 and none of them worked
heres some of the failed code with bodygyro:
(this is all on r6)

while true do
wait()
if hum.MoveDirection.Magnitude > 0 then
local dir = (torso.Position - hum.MoveDirection).unit
local look = torso.CFrame.lookVector
gyrostuff.CFrame = CFrame.new(torso.Position, torso.Position + dir)
print(hum.MoveDirection)
end
end

3 Likes
1 Like

LocalScript in StarterCharacterScripts

local RunService = game:GetService'RunService'
local Players = game:GetService'Players'
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass'Humanoid'
local RootPart = Character:WaitForChild'HumanoidRootPart'
local RootJoint = RootPart:WaitForChild'RootJoint'
local RootC0 = RootJoint.C0

local MaxTiltAngle = 10

local Tilt = CFrame.new()
RunService.RenderStepped:Connect(function(Delta)
	local MoveDirection = RootPart.CFrame:VectorToObjectSpace(Humanoid.MoveDirection)
	Tilt = Tilt:Lerp(CFrame.Angles(math.rad(-MoveDirection.Z) * MaxTiltAngle, math.rad(-MoveDirection.X) * MaxTiltAngle, 0), 0.2 ^ (1 / (Delta * 60)))
	RootJoint.C0 = RootC0 * Tilt
end)
51 Likes

You should take into account deltaTime, otherwise the torso will lerp much slower/faster depending on frame rate.

6 Likes

Fixed, thanks for the idea, I forgot about that

edit: Ok I don’t think I actually fixed it, it lerps really really slowly with over 60 fps, I forgot how to do the formula for lerping with delta time.

2 Likes

how would you only tilt if the player is moving left?

Replace -MoveDirection.Z with 0 and math.rad(-MoveDirection.X) * MaxTiltAngle with math.max(math.rad(-MoveDirection.X) * MaxTiltAngle, 0)

1 Like

for some reason, it only works if the player is moving left:

local RunService = game:GetService'RunService'
local Players = game:GetService'Players'
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass'Humanoid'
local RootPart = Character:WaitForChild'HumanoidRootPart'
local RootJoint = RootPart:WaitForChild'RootJoint'
local RootC0 = RootJoint.C0

local MaxTiltAngle = 7

local Tilt = CFrame.new()
RunService.RenderStepped:Connect(function(Delta)
	local MoveDirection = RootPart.CFrame:VectorToObjectSpace(Humanoid.MoveDirection)
	Tilt = Tilt:Lerp(CFrame.Angles(math.rad(0) * MaxTiltAngle, math.max(math.rad(-MoveDirection.X) * MaxTiltAngle, 0), 0), 0.2 ^ (1 / (Delta * 60)))
	RootJoint.C0 = RootC0 * Tilt
end)

Well, that’s what you asked for. If you want to tilt only when walking sideways then you can revert the -MoveDirection.X * MaxTiltAngle back to the original (without the math.max)

2 Likes

true, that is what i asked for, lol

3 Likes

for anyone who needs this for r15, this slightly alternate script worked decently well for me

local RunService = game:GetService'RunService'
local Players = game:GetService'Players'
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass'Humanoid'
local RootPart = Character:WaitForChild'HumanoidRootPart'
local RootJoint = Character:WaitForChild('UpperTorso'):WaitForChild('Waist')
local RootC0 = RootJoint.C0

local MaxTiltAngle = 60

local Tilt = CFrame.new()
RunService.RenderStepped:Connect(function(Delta)
	local MoveDirection = RootPart.CFrame:VectorToObjectSpace(Humanoid.MoveDirection)
	Tilt = Tilt:Lerp(
		CFrame.Angles(0,0, math.rad(-MoveDirection.X) * MaxTiltAngle)
		, 0.2 ^ (1 / (Delta * 60)))
	RootJoint.C0 = RootC0 * Tilt
end)
2 Likes

Lol, I didn’t saw it at first and changed his script for r15 by myself

local RunService = game:GetService'RunService'
local Players = game:GetService'Players'
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass'Humanoid'
local RootPart = Character:WaitForChild'HumanoidRootPart'
local RootJoint = Character.LowerTorso:WaitForChild'Root' --I changed to root joint
local RootC0 = RootJoint.C0

local MaxTiltAngle = -10 --and made it negative

local Tilt = CFrame.new()
RunService.RenderStepped:Connect(function(Delta)
	local MoveDirection = RootPart.CFrame:VectorToObjectSpace(Humanoid.MoveDirection)
	Tilt = Tilt:Lerp(CFrame.Angles(math.rad(-MoveDirection.Z) * MaxTiltAngle, math.rad(-MoveDirection.X) * MaxTiltAngle, 0), 0.2 ^ (1 / (Delta * 60)))
	RootJoint.C0 = RootC0 * Tilt
end)
1 Like