[Solved] How could I make the player tilt the way they are moving?

How can I make the player character tilt in the direction they are moving? I’m looking for a way to add a dynamic tilting effect to the player model based on their movement direction (left, right, forward, etc.). Any suggestions or scripting tips would be appreciated!

2 Likes

Try this:

local RunService = game:GetService("RunService")
local StarterPlayer = game:GetService("StarterPlayer")

-- Since this script has RunContext of Client, it will run anywhere regardless of its parent.
-- We only want it to run when it's parented to a character so we'll return immediately if it's in StarterCharacterScripts.
if script.Parent == StarterPlayer.StarterCharacterScripts then
	return
end

local character = script.Parent
-- Characters are not replicated atomically so we need to wait for children
local humanoid = character:WaitForChild("Humanoid")
local root = character:WaitForChild("HumanoidRootPart")
local rootJoint = character:WaitForChild("LowerTorso"):WaitForChild("Root")

local ROLL_ANGLE = math.rad(15)
local PITCH_ANGLE = math.rad(5)
local LEAN_SPEED = 10

local leanCFrame = CFrame.new()

local function onStepped(_: number, deltaTime: number)
	local relativeVelocity = root.CFrame:VectorToObjectSpace(root.AssemblyLinearVelocity)
	-- Calculate pitch and roll based on the character's relative velocity
	local pitch = math.clamp(relativeVelocity.Z / humanoid.WalkSpeed, -1, 1) * PITCH_ANGLE
	local roll = -math.clamp(relativeVelocity.X / humanoid.WalkSpeed, -1, 1) * ROLL_ANGLE

	leanCFrame = leanCFrame:Lerp(CFrame.Angles(pitch, 0, roll), math.min(deltaTime * LEAN_SPEED, 1))
	-- Apply the leaning to the rootJoint's Transform
	rootJoint.Transform = leanCFrame * rootJoint.Transform
end

script.Parent.HumanoidRootPart.CanCollide = false

RunService.Stepped:Connect(onStepped)

make sure it’s in StarterCharacterScripts

Does nothing.

Ended up just stealing the one from the platformer template.

--[[
	CharacterLeanScript - This script makes characters slightly lean in the direction they are moving.

	Each Step the character's Root joint Transform is updated based on the character's velocity.
--]]

local RunService = game:GetService("RunService")
local StarterPlayer = game:GetService("StarterPlayer")

-- Since this script has RunContext of Client, it will run anywhere regardless of its parent.
-- We only want it to run when it's parented to a character so we'll return immediately if it's in StarterCharacterScripts.
if script.Parent == StarterPlayer.StarterCharacterScripts then
	return
end

local character = script.Parent
-- Characters are not replicated atomically so we need to wait for children
local humanoid = character:WaitForChild("Humanoid")
local root = character:WaitForChild("HumanoidRootPart")
local rootJoint = character:WaitForChild("LowerTorso"):WaitForChild("Root")

local ROLL_ANGLE = math.rad(15)
local PITCH_ANGLE = math.rad(5)
local LEAN_SPEED = 10

local leanCFrame = CFrame.new()

local function onStepped(_: number, deltaTime: number)
	local moveVelocity = humanoid:GetMoveVelocity()
	local relativeVelocity = root.CFrame:VectorToObjectSpace(moveVelocity)

	-- Calculate pitch and roll based on the character's relative velocity
	local pitch = 0
	local roll = 0
	if humanoid.WalkSpeed ~= 0 then
		pitch = math.clamp(relativeVelocity.Z / humanoid.WalkSpeed, -1, 1) * PITCH_ANGLE
		roll = -math.clamp(relativeVelocity.X / humanoid.WalkSpeed, -1, 1) * ROLL_ANGLE
	end

	leanCFrame = leanCFrame:Lerp(CFrame.Angles(pitch, 0, roll), math.min(deltaTime * LEAN_SPEED, 1))
	-- Apply the leaning to the rootJoint's Transform
	rootJoint.Transform = leanCFrame * rootJoint.Transform
end

RunService.Stepped:Connect(onStepped)

is it posible to converte it to r6?

Use this, its 10x better

1 Like

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