I know it’s no where near to perfect, but I’d like some tips on how to polish it with the state it’s in before I start attempting to add more advanced movement features (dash, slide, etc)
Any critique, help, and advice is appreciated!
Some known bugs I haven’t been able to squash:
- When running (left shift) and holding down a movement button (w,a,s,d), if you stop pressing left shift while continuing to hold down a movement, it will not play the walk animation.
local player = game:GetService("Players").LocalPlayer
local humanoid = player.CharacterAdded:Wait().Humanoid
local camera = game:GetService("Workspace"):WaitForChild("Camera")
local originalWalkSpeed = 16
local UIS = game:GetService("UserInputService")
local TWS = game:GetService('TweenService')
local RS = game:GetService("RunService")
local fov = 70
local info = TweenInfo.new(.3, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
local goal = {FieldOfView = fov + 20}
local goal2 = {FieldOfView = fov}
local Runtween = TWS:Create(game.Workspace.CurrentCamera, info, goal)
local Runtween2 = TWS:Create(game.Workspace.CurrentCamera, info, goal2)
local runAnim = script.runAnimation
local runAnimtrack = humanoid:LoadAnimation(runAnim)
local running = false
local walking = false
local crouching = false
local crouchwalk = false
local runningInputended = false
local walkAnim = script.walkAnimation
local walkAnimtrack = humanoid:LoadAnimation(walkAnim)
local jumpAnim = script.jumpAnimation
local jumpAnimtrack = humanoid:LoadAnimation(jumpAnim)
local jumprunAnim = script.jumprunAnimation
local jumprunAnimtrack = humanoid:LoadAnimation(jumprunAnim)
local crouchAnim = script.crouchAnimation --the animation for crouching
local crouchAnimtrack = humanoid:LoadAnimation(crouchAnim)
local crouchingAnim = script.crouchingAnimation --the last frame for crouching looped to hold the crouch
local crouchingAnimtrack = humanoid:LoadAnimation(crouchingAnim)
local crouchwalkAnim = script.crouchwalkAnimation --walking while crouched animation
local crouchwalkAnimtrack = humanoid:LoadAnimation(crouchwalkAnim)
--local freefallAnim = script.freefallAnimation
--local freefallAnimtrack = humanoid:LoadAnimation(freefallAnim)
--keybind detection
UIS.InputBegan:Connect(function(IO, proc)
if proc == false and IO.KeyCode == Enum.KeyCode.LeftShift and player.Character:WaitForChild("HumanoidRootPart").Velocity.Magnitude > 1 and not running and not crouching then
running = true
Runtween:Play()
humanoid.WalkSpeed = 32
walkAnimtrack:Stop()
crouchAnimtrack:Stop()
crouchingAnimtrack:Stop()
crouchAnimtrack:Stop()
runAnimtrack:Play()
elseif proc == false and IO.KeyCode == Enum.KeyCode.C and not crouching and not running then
crouching = true
humanoid.WalkSpeed = 8
print(humanoid.WalkSpeed)
walkAnimtrack:Stop()
runAnimtrack:Stop()
crouchAnimtrack:Play()
task.wait(0.2)
crouchingAnimtrack:Play()
end
end)
UIS.InputEnded:Connect(function(IO, proc)
if proc == false and IO.KeyCode == Enum.KeyCode.LeftShift then
runningInputended = true
elseif proc == false and player.Character:WaitForChild("HumanoidRootPart").Velocity.Magnitude < 1 and IO.KeyCode ~= Enum.KeyCode.LeftShift then
print("hi")
elseif proc == false and IO.KeyCode == Enum.KeyCode.C then
crouching = false
humanoid.WalkSpeed = 16
crouchwalkAnimtrack:Stop()
crouchingAnimtrack:Stop()
crouchAnimtrack:Stop()
end
end)
function checkRunning()
--run
if running and player.Character:WaitForChild("HumanoidRootPart").Velocity.Magnitude < 1 or runningInputended or crouching then
runningInputended = false
running = false
Runtween2:Play()
humanoid.WalkSpeed = 16
runAnimtrack:Stop()
end
local magnitude = humanoid.MoveDirection.Magnitude
--walk
if not running and magnitude > 0 and not walking and not crouching then
walking = true
walkAnimtrack:Play()
elseif walking and magnitude == 0 then
-- Stop walking if the magnitude is 0
walking = false
walkAnimtrack:Stop()
end
--crouch
if crouching then
if magnitude > 0 and not crouchwalk then
crouchwalk = true
crouchAnimtrack:Stop()
crouchingAnimtrack:Stop()
crouchwalkAnimtrack:Play()
elseif magnitude == 0 then
crouchwalk = false
crouchwalkAnimtrack:Stop()
crouchingAnimtrack:Play()
end
end
end
RS.RenderStepped:Connect(checkRunning)
if not running then
if player.Character:WaitForChild("HumanoidRootPart").Velocity.Magnitude > 1 then
if not walkAnimtrack.IsPlaying then
walkAnimtrack:Play()
end
elseif walkAnimtrack.IsPlaying then
walkAnimtrack:Stop()
end
end
function checkIdle()
if humanoid.MoveDirection.Magnitude > 0 then
print("player is moving")
else
print("idle")
end
end
--jump detection
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Freefall and running then
crouchingAnimtrack:Stop()
crouchAnimtrack:Stop()
jumprunAnimtrack:Play()
elseif oldState == Enum.HumanoidStateType.Freefall and running then
runAnimtrack:Play()
elseif newState == Enum.HumanoidStateType.Freefall and not running then
crouchingAnimtrack:Stop()
crouchAnimtrack:Stop()
jumpAnimtrack:Play()
end
end)
Edit: If it’s helpful, I could showcase the animations, feel free to ask.