I made multiple movement animations for each direction W, A, S, D.
I have a locked camera and when I press a key like A, or D, the player moves sideways (strafe) BUT I need to play one of my strafe animations for each direction A and D and a walking backwards animation for S and a custom walking forwards Animation for the W key.
I already have a partial movement script (shown below) which plays different movement animations for the Character based on the speed of the Humanoid.
However, I cant figure out how to play the different movement (strafe) animations when I press A, or D.
I started learning Lua about a week ago, and finished watching Alvinblox tutorials
so I’m quite new to how this works.
NOTE: This is for a game which will also need to have walking sounds for different materials the player steps on and there will be 2 different falling animations playing if the player has been falling for more or less than 3 seconds, therefore I’m trying to code with these in mind too. I think these are unrelated to the strafe however!?
MY train of thought for a solution to this is that I should be detecting what direction the player is moving in then play the animation accordingly!? Is that efficient? Can it be Exploited?
However I have no idea how to do that.
I need some help.
Animations:
Strafe Left Animation There is a Strafing to the right too.
Walking Backwards Animation There is also a Walking forwards Animation.
Image:
This is my current code:
--Service References
local RunService = game:GetService("RunService")
--Variables
local Player = game.Players.LocalPlayer
local Jumped = false
local CurrentAnimation
--Animation tracks
local Humanoid = Player.Character:WaitForChild("Humanoid")
local WalkAnim = Humanoid:LoadAnimation(script:WaitForChild("Walk"))
local RunAnim = Humanoid:LoadAnimation(script:WaitForChild("Run"))
local IdleAnim = Humanoid:LoadAnimation(script:WaitForChild("Idle"))
local JumpAnim = Humanoid:LoadAnimation(script:WaitForChild("Jump"))
local FallAnim = Humanoid:LoadAnimation(script:WaitForChild("Fall"))
--Events
local SpeedChangeEvent
local StateChangedEvent
local function PlayAnimation(Animation, Speed)
Speed = Speed or 1
if CurrentAnimation then
CurrentAnimation:Stop()
end
CurrentAnimation = Animation
Animation:Play(0.1, 1, Speed)
end
local function SpeedChange(Speed)
if Speed == 0 then --Plays the appropriate idle animation when the player's idle
PlayAnimation(IdleAnim)
elseif Humanoid.WalkSpeed == 10 and not WalkAnim.IsPlaying then --Plays the appropriate movement animation depending on the player's walk speed and state
PlayAnimation(WalkAnim, 1.3)
elseif Humanoid.WalkSpeed == 16 and not RunAnim.IsPlaying then
PlayAnimation(RunAnim, 1.5)
end
end
local function StateChanged(OldState, NewState)
if NewState == Enum.HumanoidStateType.Freefall then
Jumped = true
PlayAnimation(JumpAnim)
--Starts a counter where if jumped is still true after a second the falling animation will play
local Start = tick()
while Jumped do
if tick() - Start >= 1 then
PlayAnimation(FallAnim)
break
end
RunService.Heartbeat:Wait()
end
elseif NewState == Enum.HumanoidStateType.Landed then
Jumped = false
end
end
--If the character isn't frozen when he/she spawns in then connect the events to the functions
SpeedChangeEvent = Humanoid.Running:Connect(SpeedChange)
StateChangedEvent = Humanoid.StateChanged:Connect(StateChanged)
Thanks for taking your time to read!