You're right. Here's a corrected version that ensures more natural arm and leg swinging:
-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Get the player and wait for the character to load
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
-- Wait for the humanoid to exist
local humanoid = character:WaitForChild("Humanoid")
-- Determine if the character is R15 or R6
local isR15 = humanoid.RigType == Enum.HumanoidRigType.R15
-- Get the root part and humanoid root part
local rootPart = character:WaitForChild(isR15 and "UpperTorso" or "Torso")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- Get the key joints
local neck, leftShoulder, rightShoulder, leftHip, rightHip
if isR15 then
local root = humanoidRootPart:WaitForChild("Root")
neck = root:WaitForChild("Neck")
leftShoulder = character:WaitForChild("LeftUpperArm"):WaitForChild("LeftShoulder")
rightShoulder = character:WaitForChild("RightUpperArm"):WaitForChild("RightShoulder")
leftHip = root:WaitForChild("LeftHip")
rightHip = root:WaitForChild("RightHip")
else
neck = rootPart:WaitForChild("Neck")
leftShoulder = rootPart:WaitForChild("Left Shoulder")
rightShoulder = rootPart:WaitForChild("Right Shoulder")
leftHip = rootPart:WaitForChild("Left Hip")
rightHip = rootPart:WaitForChild("Right Hip")
end
-- Store the original C0 offsets for each joint so we can apply offsets on top
local origNeckC0 = neck.C0
local origLSC0 = leftShoulder.C0
local origRSC0 = rightShoulder.C0
local origLHC0 = leftHip.C0
local origRHC0 = rightHip.C0
-- Parameters (tweak these to fit your character's style)
local idleBreatheSpeed = 2 -- Frequency for idle "breathing"
local idleBreatheAmount = math.rad(2) -- Small head nod amount for idle
local walkSpeedFactor = 8 -- Frequency multiplier for walking swings
local walkSwingAmount = math.rad(15) -- Base amplitude for arm/leg swings
local torsoTwistAmount = math.rad(3) -- Slight torso twist for weight shift
local headBobAmount = math.rad(2) -- Subtle head bobbing
local jumpArmAngle = math.rad(45) -- Angle to raise arms during jump
-- Timer variable for our animation
local elapsedTime = 0
-- Main animation loop (using RenderStepped for smoother animation)
RunService.RenderStepped:Connect(function(dt)
elapsedTime = elapsedTime + dt
-- Determine the current state
local currentState = humanoid:GetState()
local velocityMag = humanoidRootPart.Velocity.Magnitude
if currentState == Enum.HumanoidStateType.Jumping or currentState == Enum.HumanoidStateType.Freefall then
-- === JUMP POSE ===
leftShoulder.C0 = origLSC0 * CFrame.Angles(-jumpArmAngle, 0, 0)
rightShoulder.C0 = origRSC0 * CFrame.Angles(-jumpArmAngle, 0, 0)
leftHip.C0 = origLHC0
rightHip.C0 = origRHC0
neck.C0 = origNeckC0
elseif velocityMag < 1 then
-- === IDLE POSE ===
local breathe = math.sin(elapsedTime * idleBreatheSpeed) * idleBreatheAmount
neck.C0 = origNeckC0 * CFrame.Angles(breathe, 0, 0)
leftShoulder.C0 = origLSC0
rightShoulder.C0 = origRSC0
leftHip.C0 = origLHC0
rightHip.C0 = origRHC0
else
-- === WALKING POSE (More Natural) ===
-- Use a sine wave to drive the swing with opposite phases
local armSwing = math.sin(elapsedTime * walkSpeedFactor)
local legSwing = math.sin(elapsedTime * walkSpeedFactor + math.pi)
-- Arms: Swing in opposite directions
leftShoulder.C0 = origLSC0 * CFrame.Angles(0, 0, armSwing * walkSwingAmount)
rightShoulder.C0 = origRSC0 * CFrame.Angles(0, 0, -armSwing * walkSwingAmount)
-- Legs: Swing in opposite directions
leftHip.C0 = origLHC0 * CFrame.Angles(legSwing * walkSwingAmount, 0, 0)
rightHip.C0 = origRHC0 * CFrame.Angles(-legSwing * walkSwingAmount, 0, 0)
-- Optional torso twist for a subtle weight shift:
local twist = math.sin(elapsedTime * walkSpeedFactor) * torsoTwistAmount
-- Instead of modifying torso.CFrame, we modify the Root C0 to achieve a similar effect.
if isR15 then
humanoidRootPart.RootJoint.C0 = humanoidRootPart.RootJoint.C0 * CFrame.Angles(0, twist, 0)
end
-- Head bobbing: a subtle up and down motion.
local headBob = math.sin(elapsedTime * walkSpeedFactor * 0.5) * headBobAmount
neck.C0 = origNeckC0 * CFrame.Angles(headBob, 0, 0)
end
end)
Key changes:
-
Arms Swing:
- Now using
armSwing to create opposite directional swings
- Left arm swings positive, right arm swings negative
- This creates a more natural walking arm motion
-
Legs Swing:
- Using
legSwing with a phase shift (+ math.pi)
- Left leg swings positive, right leg swings negative
- This ensures legs move in opposite directions, mimicking natural walking
-
Phase Shifting:
- By using
math.sin(elapsedTime * walkSpeedFactor + math.pi) for legs, we create a 180-degree phase shift
- This ensures arms and legs move in a more realistic walking pattern
The script now provides a more natural walking animation where:
- Arms swing in opposite directions
- Legs move in opposite directions
- The motion mimics a more realistic walking cycle
You can further fine-tune the walkSpeedFactor and walkSwingAmount to adjust the animation's intensity and speed to match your specific character design.