- What do you want to achieve? Keep it simple and clear!
I want to create a directional movement system and make it so I can switch my animations (Right, left, forward, backwards) while moving.
- What is the issue? Include screenshots / videos if possible!
I edited Roblox’s “Animate” script and there animations play when events fire.
Humanoid.Running event fires again only when you stop holding any key that moves your character . That’s why it doesn’t switch animations when I keep holding at least one button (W, A, S, D)
Here’re the functions that are responsible for directional movement:
function CalculateMovement()
local DirectionOfMovement = HRP.CFrame:VectorToObjectSpace( HRP.AssemblyLinearVelocity )
local walkspeed = Humanoid.WalkSpeed
local Forward = math.abs( math.clamp( DirectionOfMovement.Z / walkspeed, -1, 0 ) )
local Backwards = math.abs( math.clamp( DirectionOfMovement.Z / walkspeed, 0, 1 ) )
local Right = math.abs( math.clamp( DirectionOfMovement.X / walkspeed, 0, 1 ) )
local Left = math.abs( math.clamp( DirectionOfMovement.X / walkspeed, -1, 0 ) )
local speed = DirectionOfMovement.Magnitude
local anim
if speed > .75 then
local scale = 16.0
if Forward > 0.5 then
anim = "Forward"
elseif Forward < 0.5 and Backwards < 0.5 then
if Left >= 0.5 then
anim = "Left"
elseif Right >= 0.5 then
anim = "Right"
end
elseif Backwards >= 0.5 then
anim = "Backwards"
end
end
return anim, speed
end
function onRunning()
local direction, speed = CalculateMovement()
local scale = 16
if direction == "Forward" then
playAnimation("walkforward", 0.2, Humanoid)
setAnimationSpeed(speed / scale)
elseif direction == "Left" then
playAnimation("walkleft", 0.2, Humanoid)
setAnimationSpeed(speed / scale)
elseif direction == "Right" then
playAnimation("walkright", 0.2, Humanoid)
setAnimationSpeed(speed / scale)
elseif direction == "Backwards" then
playAnimation("walkbackwards", 0.2, Humanoid)
setAnimationSpeed(speed / scale)
else
playAnimation("idle", 0.2, Humanoid)
pose = "Standing"
end
end
Humanoid.Running:Connect(onRunning)
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried to use RunService. It works but it breaks other animations that I need, like jumping, since they are called by events like Humanoid.Jumping.
Here is my Animate script:
Animate.rbxm (11.6 KB)
Would appreciate any help!