So currently I’m working on movement set, and I have made this far until crouching animation.
What I want is to switch animation based on player’s walkSpeed. If the player is moving, the Movement animation will run. Otherwise, the Idle one will take place. However, when I stopped moving while crouching, the Movement still runs. I checked my Move detection code and it worked properly but the animation.
I actually found a solution for this before, using RenderStepped to track player’s moveDirection. But it comes with a problem: It worsen the game’s performance overtime. After that, I tried searching other solutions, and none of them works.
Here is my current script in StarterCharacterScripts
local run = game:GetService('RunService')
local UIS = game:GetService('UserInputService')
local tween = game:GetService('TweenService')
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character
local humanoid = character.Humanoid
local animator = humanoid:FindFirstChildOfClass("Animator")
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
local timer
--numberical variables
local baseSpeed = 10
local sprintSpeed = 22
local crouchSpeed = 6
--boolean variables
local crouching = false
local runnable = false
local aiming, running
--setup
humanoid.CameraOffset = Vector3.new(0, .6, 0)
local tweenInfo = TweenInfo.new(0.3,Enum.EasingStyle.Quad, Enum.EasingDirection.Out,0)
local goal = {}
--animation
local crouch1 = Instance.new('Animation')
crouch1.AnimationId = "rbxassetid://6314087082"
local crouchIdle = humanoid:LoadAnimation(crouch1)
local crouch2 = Instance.new('Animation')
crouch2.AnimationId = "rbxassetid://7644044295"
local crouchAnim = humanoid:LoadAnimation(crouch2)
--codes
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift and not crouching and not aiming then
humanoid.WalkSpeed = sprintSpeed
--Crouching plays
else if input.KeyCode == Enum.KeyCode.LeftControl then
crouching = true
humanoid.WalkSpeed = crouchSpeed
goal.CameraOffset = Vector3.new(0,-1.8, 0)
local crouchTween = tween:Create(humanoid, tweenInfo, goal)
crouchTween:Play()
humanoidRootPart.Size = Vector3.new(2,1.2,1)
--Determine which animation to play
if running then
crouchAnim:Play()
crouchIdle:Stop()
elseif not running then
crouchAnim:Play()
crouchIdle:Play()
end
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
humanoid.WalkSpeed = baseSpeed
else if input.KeyCode == Enum.KeyCode.LeftControl then
crouching = false
humanoid.WalkSpeed = baseSpeed
goal.CameraOffset = Vector3.new(0,.6, 0)
humanoidRootPart.Size = Vector3.new(2,2.1,1)
local crouchTween = tween:Create(humanoid, tweenInfo, goal)
crouchTween:Play()
crouchIdle:Stop()
crouchAnim:Stop()
end
end
end)
--Track player's speed
humanoid.Running:Connect(function(speed)
if speed > 0 then
running = true
elseif speed == 0 then
running = false
end
end)
Output:
robloxapp-20220520-0922362.wmv (1.5 MB)
If you spot any mistakes or have any suggestions, please tell me. As an newcomer, I’m willing to learn. Thank you!