How do I create tilted movement?

For example, when the player moves left, they will tilt left, and when the player moves right, they will tilt to the right?

Could try this:

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


local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local Torso = Character.Torso

local RootJoint = HumanoidRootPart.RootJoint
local LeftHipJoint = Torso["Left Hip"]
local RightHipJoint = Torso["Right Hip"]

local function Lerp(a, b, c)
	return a + (b - a) * c
end

local Force = nil
local Direction = nil
local Value1 = 0
local Value2 = 0

local RootJointC0 = RootJoint.C0
local LeftHipJointC0 = LeftHipJoint.C0
local RightHipJointC0 = RightHipJoint.C0

RunService.RenderStepped:Connect(function()
	--> To get the force, we multiply the velocity by 1,0,1, we don't want the Y value so we set y to 0
	Force = HumanoidRootPart.Velocity * Vector3.new(1,0,1)
	if Force.Magnitude > 0.001 then
		--> This represents the direction
		Direction = Force.Unit	
		Value1 = HumanoidRootPart.CFrame.RightVector:Dot(Direction)
		Value2 = HumanoidRootPart.CFrame.LookVector:Dot(Direction)
	else
		Value1 = 0
		Value2 = 0
	end

	--> the values being multiplied are how much you want to rotate by

	RootJoint.C0 = RootJoint.C0:Lerp(RootJointC0 * CFrame.Angles(math.rad(Value2 * 5), math.rad(-Value1 * 10), 0), 0.2)
	LeftHipJoint.C0 = LeftHipJoint.C0:Lerp(LeftHipJointC0 * CFrame.Angles(math.rad(Value1 * 4), 0, 0), 0.2)
	RightHipJoint.C0 = RightHipJoint.C0:Lerp(RightHipJointC0 * CFrame.Angles(math.rad(-Value1 * 4), 0, 0), 0.2)
end)

07f747360ab8c21e28bd66fbcf914b94

-- Services
local RunService = game:GetService("RunService")

-- Constants
local MOMENTUM_FACTOR = 0.02
local MIN_MOMENTUM = 0
local MAX_MOMENTUM = math.huge
local SPEED = 15

-- Function to calculate momentum
local function calculateMomentum(velocity)
    -- Calculate momentum based on velocity
    return Vector3.new(
        math.clamp(math.abs(velocity.X), MIN_MOMENTUM, MAX_MOMENTUM),
        0,
        math.clamp(math.abs(velocity.Z), MIN_MOMENTUM, MAX_MOMENTUM)
    ) * MOMENTUM_FACTOR
end

-- Function to calculate angles
local function calculateAngles(direction, momentum, rigType)
    -- Calculate angles for rotation based on direction, momentum, and rig type
    local x = direction.X * momentum.X
    local z = direction.Z * momentum.Z
    return rigType == Enum.HumanoidRigType.R15 and {z, 0, -x} or {-z, -x, 0}
end

-- Function to update m6d.C0
local function updateM6dC0(m6d, originalM6dC0, angles, dt)
    -- Interpolate and update the position and orientation of m6d.C0
    m6d.C0 = m6d.C0:Lerp(originalM6dC0 * CFrame.Angles(unpack(angles)), dt * SPEED)
end

-- Main function
local function main()
    -- Get the character
    local character = script.Parent
    if character:IsA('Model') then
        -- Get humanoid and humanoidRootPart
        local humanoid = character:WaitForChild('Humanoid')
        local humanoidRootPart = character.HumanoidRootPart

        -- Determine m6d based on RigType
        local m6d = humanoid.RigType == Enum.HumanoidRigType.R15 and character.LowerTorso.Root or humanoidRootPart.RootJoint
        local originalM6dC0 = m6d.C0

        -- Heartbeat connection to update character movement
        RunService.Heartbeat:Connect(function(dt)
            -- Calculate direction and velocity
            local direction = humanoidRootPart.CFrame:VectorToObjectSpace(humanoid.MoveDirection)
            local velocity = humanoidRootPart.Velocity

            -- Calculate momentum and angles
            local momentum = calculateMomentum(velocity)
            local angles = calculateAngles(direction, momentum, humanoid.RigType)

            -- Update m6d.C0
            updateM6dC0(m6d, originalM6dC0, angles, dt)
        end)
    end
end

-- Run main function
main()


1 Like

It works, but I have absolutely no idea what is going on in that script, especially with that lerp function and the dot function, how do people understand this?